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;
}
}
}
}
}
});
JSFIDDLE
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;
}
}
}
}
}
});
JSFIDDLE
Share Improve this question edited Jul 21, 2014 at 12:53 Jorge B. asked Jul 21, 2014 at 11:25 Jorge B.Jorge B. 1,2132 gold badges18 silver badges38 bronze badges 4- please explain what is pt ? what do u want ur code to do – Pratik Joshi Commented Jul 21, 2014 at 11:31
-
@jQueryAngryBird PT is the value for Portugal option. If I chose Portugal the maxlength and minlength of field go to 9, else
maxlength
=50 andminlength
=3. My field is Zip code. – Jorge B. Commented Jul 21, 2014 at 12:28 - 1 Your Question is little Complicated :) I wil hav to see properly at home :) m in office now ,wait for 2 hrs. – Pratik Joshi Commented Jul 21, 2014 at 12:32
- @jQueryAngryBird you have an answer? – Jorge B. Commented Jul 21, 2014 at 15:38
1 Answer
Reset to default 4Answer Updated.
I have made some changes, removed depends
.
Here is the updated fiddle. http://jsfiddle/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;
}
}
}
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745338252a4623206.html
评论列表(0条)