可能是一个愚蠢的问题,但我似乎无法在谷歌土地上找到任何东西.我只需要一种方法来忽略输入字段的情况.我正在进行城市名称匹配,但需要"亚特兰大"和"亚特兰大"都有效.我该怎么编辑这个以获得我需要的验证爱?
jQuery.validator.addMethod("atlanta", function(value) {
return value == "Atlanta"; //Need 'atlanta' to work too
}, '**Recipient must reside in Chicago City Limits**');
Run Code Online (Sandbox Code Playgroud)
感谢任何和所有:)
我假设我需要一个正则表达式?
我有一个邮政编码字段,邮政编码必须在芝加哥的城市范围内.幸运的是,所有的邮政编码都以606开头.所以我需要轮询输入以确保输入的邮政编码是5位数,并以数字606开头:
我的输入很基本:
<label for="dzip"><span class="red">♥ </span>Zip:</label>
<input name="attributes[address_zip]" id="dzip" type="text" size="30" class="required zip-code" />
Run Code Online (Sandbox Code Playgroud)
然后我的城市脚本很容易.我只需要将它应用于邮政编码:
jQuery.validator.addMethod("Chicago", function(value) {
return value == "Chicago";
}, '**Recipient must reside in Chicago City Limits**');
Run Code Online (Sandbox Code Playgroud)
如果我展示插件如何用于电话号码(美国),可能会有所帮助.基本上我需要把它翻译成拉链:
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");
Run Code Online (Sandbox Code Playgroud)
这部分的意思是什么?
phone_number.replace(/\s+/g, "")
Run Code Online (Sandbox Code Playgroud)
我直接从验证网站上的示例中删除了手机部分.
这里所有伟大(和快速)输入的最终答案是:
jQuery.validator.addMethod("zip-code", function(zip_code, element) {
zip_code = zip_code.replace(/\s+/g, "");
return this.optional(element) || zip_code.length == 5 && zip_code.match(^606[0-9]{2}$);
}, "Please specify a City …Run Code Online (Sandbox Code Playgroud)