我通常只需使用一个下拉列表,但客户端已经要求它是一个文本输入…
基本上,我想使用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