I'm using JQuery Validation Plugin and i want change the minlength
and maxlength
of my field (zip code) that depends if country is equal to PT (Portugal) or not.
But in validation the result are always:
Anyone knows what is my problem?
HTML:
<body>
<form id="myform">
<label for="field">Required, minimum 3(Other) or 9(PT): </label>
<input type="text" class="left" id="field" name="field"/>
<br/>
<select class="form-control" id="country" name="country">
<option value="0">-- Choose Country --</option>
<option value="BE">Belgium </option>
<option value="BR">Brazil </option>
<option value="ES">Spain </option>
<option value="FR">France </option>
<option value="NL">Dutch </option>
<option selected="selected" value="PT">Portugal </option>
</select>
<br/>
<input type="submit" value="Validate!"/>
</form>
</body>
JQuery:
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$("#myform").validate({
rules: {
field: {
required: true,
number: {
depends: function () {
return !($('select[name="country"]').val() != 'PT');
}
},
minlength: {
depends: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 3;
}
}
},
maxlength: {
depends: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 50;
}
}
}
}
}
});
Answer Updated.
I have made some changes, removed depends
.
Here is the updated fiddle. http://jsfiddle.net/Y2H9d/36/
$("#myform").validate({
rules: {
field: {
required: true,
number: function () {
return !($('select[name="country"]').val() != 'PT');
},
minlength: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 3;
}
},
maxlength: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 50;
}
}
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.