问题
I am using jQuery Tools Validator to ensure that my form fields are not blank as follows:
$(document).ready(function(){
$.tools.validator.fn("#field1", function(input, value) {
return value != '' ? true : {
en: "This field is required"
};
});
$.tools.validator.fn("#field2", function(input, value) {
return value != '' ? true : {
en: "This field is required"
};
});
var form = $("#form").validator({
position: 'bottom left',
offset: [5, 0],
messageClass:'form-error',
message: '<div><em/></div>'
}).attr('novalidate', 'novalidate');
});
I need users to enter text in either one of the two fields (or both) but I am not sure how to write a matcher for the two fields.
回答1:
If you want either or both, why not:
$.tools.validator.fn("#field1", function(input, value) {
return value !== '' || $('#field2').val() !== '' ? true : {
en: "This field is required"
};
});
//vice-versa for field2
来源:https://stackoverflow.com/questions/5935269/jquery-tools-validator-require-input-in-either-of-two-fields