I have a form, where a user has to check at least one checkbox before submitting the form. Is there any plugin that can handle this or maybe jquery that can be applied to my form? Unfortunately I'm a total jquery rookie.
All the answers above show how to do this client-side, which may indeed be preferable. As your question title indicates you're using Rails 3, you could also validate this server-side:
class Example < ActiveRecord::Base
...
validates :something_was_checked
...
private
def something_was_checked
if self.checkbox_attribute.blank?
self.errors.add(:checkbox_attribute, "You must select at least one option.")
end
end
end
You can use a simple jQuery to do that
$("form").submit(function(){
if($(this).find("input:checked").length == 0){
//No checkbox checked
return false;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With