jQuery Validation rule based on select value
jQuery Validation rule based on select value
I have the following form.
<form id="myform">
<select id="country" name="country">
<option value="US">United States</option>
<option value="CA">Canada</option>
<option value="AU">Australia</option>
<option value="NZ">New Zealand</option>
<option value="IE">Ireland</option>
</select>
<input type="text" name="zip_code" id="zip-code">
<input type="submit">
</form>
I want the zip_code
field to contain the digits: true
rule when the selected country is United States
. This is what I have tried based on this code.
zip_code
digits: true
United States
$("$myform").validate({
rules:{
zip_code: {
required: true,
maxlength: 10,
digits: function(element){
return $("#country").val() == 'US';
}
}
}
});
However this isn't working. When I type letters the form still submits.
1 Answer
1
And if you set the id of the zip code input to zip_code
?
zip_code
like that : <input type="text" name="zip_code" id="zip_code">
<input type="text" name="zip_code" id="zip_code">
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.