I need some guidance on how to add a validation method to the jquery validate plugin. I've gathered that i need to use the plugin's addMethod() function, but how exactly to have it do what i need... Here i am!
My form 's first question is a radio input choice. Each radio input, if selected, shows a sub-question of several checkboxes.
What i would like my validation to do is: - make sure one radio input is selected - make sure at least one checkbox pertaining to the radio input is selected.
Basically, out of the validate plugin's own logic, i would count the selector's length, and if there isn't any, retur开发者_StackOverflow社区n the error message. Something like this:
var weekSelected = ($('input.seasonSelector:checked input.weekSelector:checked',form).length > 0);
if(!weekSelected){
return 'Please select a week inside that season';
}
How to turn that into a validate plugin method ?
UPDATE
here is the successful answer, based on your feedback:
$('#newInscription').validate({
rules: {
"season_id": "required",
"weeks[]": {
required: {
depends: function() {
return $(this).parents(".seasonSelector:checked");
}
}
}
}
,
...
Why not make the radio a required element and each of the sub-questions a required element, but dependent on the related radio? You should be able to do this by just adding rules. It will be easier if each group of checkboxes have the same name.
$("form").validate({
rules: {
"Season": "required",
"SpringWeeks": {
required: {
depends: function() {
return $(".seasonSelector:checked").val() == "Spring";
}
}
}
...
});
<input type="radio" name="Season" value="Spring" />
...
精彩评论