Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validate plugin require field if another field has a value and vice versa

I am using the jQuery validate plugin to validate my form. I would like to tie several pairs of fields together such that if one field has a value, the other is required to have a value also. Essentially both fields (both text inputs) must either both have a value or both not have a value. Is anyone aware of a good way to accomplish this?

like image 393
zaq Avatar asked May 02 '12 00:05

zaq


1 Answers

if you look at the "rules" section's example code in the documentation page, there is a depends field you can set.

something like the following (this is right off my head, not tested):

... secondInput: {     required: function(element){             return $("#firstInput").val()!="";         } } .... 
like image 50
Erico Chan Avatar answered Sep 29 '22 11:09

Erico Chan