【问题标题】:Help with jQuery validate and a Zip code restriction based on the 1st three digitss帮助 jQuery 验证和基于前三位数字的邮政编码限制
【发布时间】:2011-02-17 22:20:09
【问题描述】:

我假设我在这里需要一个正则表达式?

我有一个邮政编码字段,并且邮政编码必须在芝加哥市区范围内。幸运的是所有邮政编码都以 606 开头。所以我需要轮询输入以确保输入的邮政编码是 5 位数字并以数字 606 开头:

我的输入足够基本:

<label for="dzip"><span class="red">&hearts;&nbsp;</span>Zip:</label>
<input name="attributes[address_zip]" id="dzip" type="text" size="30" class="required zip-code" />

然后我的城市脚本就很简单了。我也只需要将其应用于邮政编码:

    jQuery.validator.addMethod("Chicago", function(value) {
    return value == "Chicago";
}, '**Recipient must reside in Chicago City Limits**');

如果我展示了电话号码插件的功能(美国),可能会有所帮助。基本上我需要把它翻译成拉链:

    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");

这部分的作用/意思是什么?:

phone_number.replace(/\s+/g, "")

我直接从验证网站上的示例中放入了电话部分。

所有伟大(和快速)输入的最终答案是这样的:

    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 of Chicago Zip Code");

【问题讨论】:

  • 从内存中 phone_number.replace(/\s+/g, "") 删除所有空格字符。
  • 顺便说一句,杰米,因为正则表达式你不需要 .length == 5 因为如果它不是 5 位数字,正则表达式就会失败。

标签: jquery jquery-validate validation


【解决方案1】:

长度使用.length property

if($('#dzip').val().length != 5)
  return false;

对于 606,请使用 substr method

if($('#dzip').val().substr(0,3) != 606)
  return false;

结合

$('#dzip').change(function(){
  var val = $(this).val();
  if(val.length != 5 || val.substr(0,3) != 606)
    return false; //not valid
  else
  //do stuff here
})


试试这个:
jQuery.validator.addMethod("chicago", function(zip, element) {
    zip = zip.replace(/\s+/g, "");
    return this.optional(element) || zip.match("^606[0-9]{2}$");
}, '**Recipient must reside in Chicago City Limits**');

上面的代码可以读作

  • 将方法chicago 添加到validator 插件
  • 当调用该方法时,去除所有空白字符
  • 如果该字段不是必需的并且为空
  • 或者如果它是必需的且不为空且长度为 5 位
  • 前3个字符是606
  • 返回真
  • 否则返回false

【讨论】:

  • 那些是伟大的冰雹!但是不要使用已经连接到表单中的验证插件。知道如何将其转换为电话号码示例吗?感谢您的参与!
  • 这很棒。我喜欢这个地方。用你的位和胶囊中的正则表达式,这个线程得到了回答!
  • 这是结合了所有优秀反馈的最终解决方案:jQuery.validator.addMethod("zip-code", function(zip_code, element) { phone_number = zip_code.replace(/\s+/g , ""); return this.optional(element) || zip_code.length == 6 && zip_code.match(^606[0-9]{2}$); }, "请指定芝加哥市邮政编码" );
  • 你能用 Capsules 正则表达式更新你的块吗,我可以检查你是否作为历史记录的答案 :) 我在我的线程中发布了组合位。
  • 杰米,你的phone_number = zip_code.replace(/\s+/g, ""); 应该是zip_code = zip_code.replace(/\s+/g, "");
【解决方案2】:

【讨论】:

  • 这就是我正在使用的,只是不确定如何将邮政编码场景写入其中。
【解决方案3】:

你可以使用这个正则表达式“606[0-9][0-9]”,看看它是否可以匹配。然后你检查输入是否是五位数,因为我认为这个正则表达式也会匹配 606111。

【讨论】:

  • "^606[0-9]{2}$" 如果您想要 606 后的 2 位数字
  • 辉煌胶囊!只是我需要一起完成捏造的部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多