|
Mailing Lists
|
Home /
Groups /
JQuery
one of three formfields required
I've got a small validation question. What's the best/easiest way to validate a form to make sure that one of three different fields have a value.Michael Dinowitz 03/22/11 08:14 P I think this will work:Rick Faircloth 03/22/11 09:15 P I've got a small validation question. What's the best/easiest way to validate a form to make sure that one of three different fields have a value. For example, I have three phone number fields (home,work,cell). I'd like a phone number in at least one of those fields. I can do it on the server side but I'd prefer the client as well. Thanks I think this will work: if ( $('#field1').length == 0 && $('#field2').length == 0 && $('#field3').length == 0 ) { $('#errorMessage').show(); } You've got to have something in at least one of the fields to not get the error message. Poking around a bit, I found the 'empty' method, too: if ( $('#field1').is(':empty') && $('#field2').is(':empty') && $('#field3').is(':empty') ) { $('#errorMessage').show(); } Even shorter: if ( !$('#field1').val() && !$('#field2').val() && !$('#field3').val() ) { $('#errorMessage').show(); } Need more explicit code? Rick I've got a small validation question. What's the best/easiest way to validate a form to make sure that one of three different fields have a value. For example, I have three phone number fields (home,work,cell). I'd like a phone number in at least one of those fields. I can do it on the server side but I'd prefer the client as well. Thanks
|
May 21, 2013
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||