1) As per the question you’ve linked, I constructed this jsFiddle using the depends
option and still see lots of issues. The rules do not seem to work at all, where both fields are always required.
2) There is a require_from_group
rule included in the additional-methods.js
file. The two fields must have the same class
for the rule to target, in this case, .group
. But this method has some known bugs where it disables all of the other rules on the form
DEMO: http://jsfiddle.net/SvSrR/
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
groups: {
name: "telephone mobile"
},
rules: {
telephone: {
require_from_group: [1, '.group']
},
mobile: {
require_from_group: [1, '.group']
}
}
});
});
3) Instead, I constructed a custom rule called customrule
(you can name it however you want) and it’s working as I believe it should. I also used the groups
option to combine the two error messages into one.
Working DEMO: http://jsfiddle.net/2g8hL/
$(document).ready(function () {
$.validator.addMethod("customrule", function (value, element) {
return (!($("#mobile").val() === '') || !($("#telephone").val() === ''));
}, "please fill out at least one");
$('#myform').validate({ // initialize the plugin
groups: {
name: "telephone mobile"
},
rules: {
telephone: {
customrule: true
},
mobile: {
customrule: true
}
}
});
});
4
solved jQuery Validate plugin : Only one field required out of multiple [closed]