Javascript英国邮政编码验证 [英] Javascript UK Postcode Validation

查看:117
本文介绍了Javascript英国邮政编码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

英国邮政编码正则表达式(综合)

我有以下代码:在javascript中验证邮政编码:

I have the following code for validating postcodes in 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

我做了大量的研究,但似乎无法找到这个常见验证要求的权威正则表达式。有人能指出我正确的方向吗?

I have done a ton of research but can't seem to find the definitive regex for this common validation requirement. Could someone point me in the right direction please?

推荐答案

考虑到英国的所有排列,你想要一个'权威的正则表达式'邮政编码因此需要不必要的大。这是我过去使用的一个

You want a 'definitive regex' - given all the permutations of the UK postcodes, it needs to be therefore 'unnecessarily large'. Here's one I've used in the past

"(GIR 0AA)|((([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][0-9]?)|(([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][0-9]?)|(([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][ABCDEFGHJKSTUW])|([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][ABEHMNPRVWXY])))) [0-9][ABDEFGHJLNPQRSTUWXYZ]{2})"

注意我从不使用AZ,例如,因为在每个部分都总是排除某些字母。

Notice I never just use A-Z, for instance, because in each part there are always certain letters excluded.

这篇关于Javascript英国邮政编码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆