admin管理员组文章数量:1420973
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;
}
}
}
}
});
本文标签: javascriptJQuery validate field number length dependsStack Overflow
版权声明:本文标题:javascript - JQuery validate field number length depends - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745338252a2654141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论