jquery validation or condition
Hi I am trying to do form validatio开发者_运维技巧n and I would like to use something similar to this: http://jquery.bassistance.de/validate/demo/errorcontainer-demo.html Except How can I get it to only display an error message if either the phone field or address field isn't provided? Unlike the example I want to just display an error message for phone/address only if one of them isn't filled in
Take a look at the options for "required". You can specify a dependency expression or a dependency callback. This means you are providing a bit of javascript code (within the class attribute, where the example just says required:true
), and you're saying "if my bit of code evaluates as true, then this form element is required".
So in your case, both the address and the phone fields would need dependency expressions that say "if the other one is empty, this one is required".
Well, if you look at the example, the validation is applied on the input itself in the class
attribute:
<input name="phone" id="phone" class="some class {validate:{required:true,number:true, rangelength:[2,8]}}" />
You only need to put the validation on the fields you want via this method.
What you could do is remove the validation from the other field if one is populated:
$('#phone').change(function(){
if($(this).val().length > 0)
{
$('#address').removeClass().addClass('original class without validation');
}
});
精彩评论