Jquery Validator – 根据接受的值列表检查输入

前端之家收集整理的这篇文章主要介绍了Jquery Validator – 根据接受的值列表检查输入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我通常只需使用一个下拉列表,但客户端已经要求它是一个文本输入…

基本上,我想使用Jquery验证器来检查用户是否已经在文本输入框中输入了50个有效的美国州名缩写中的1个.如果没有,他们会收到错误.我似乎找不到这样做的功能.任何帮助是极大的赞赏!

解决方法

这是一个验证方法,您可以添加jquery validate一起使用

首先你声明一个验证器:

jQuery.validator.addMethod("isstate",function(value) {
    var states = [
        "AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","AS","DC","FM","GU","MH","MP","PR","PW","VI"
    ];
    return $.inArray(value.toUpperCase(),states) != -1;
},"Data provided is not a valid state");

然后应用验证器

$("#myform").validate()

并且在您的表单中,您要将类添加到要检查的表单元素

<input type="text" value="de" class="isstate" />

这是一个working demo

原文链接:https://www.f2er.com/jquery/176499.html

猜你在找的jQuery相关文章