【发布时间】:2012-12-07 19:30:59
【问题描述】:
我有以下代码用于在 javascript 中验证邮政编码:
function valid_postcode(postcode) {
postcode = postcode.replace(/\s/g, "");
var regex = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
return regex.test(postcode);
}
测试:
CF47 0HW - Passes - Correct
CF47 OHW - Passes - Incorrect
我已经进行了大量研究,但似乎无法为这个常见的验证要求找到最终的正则表达式。有人能指出我正确的方向吗?
【问题讨论】:
-
我已经使用了这个测试器:regular-expressions.info/javascriptexample.html 并且您提供的示例按预期工作。第一个通过,第二个没有。
-
@willemD'haeseleer 我已经驳回了那个脚本,因为在我看来,它太大了。
-
@jnovacho 对于 Safari 6 中的两个示例,我都得到“不匹配”。
标签: javascript regex validation postal-code