问题描述
所以我想编写一个检查html输入文本中的邮政编码的验证。我想检查邮政编码是否来自特定状态然后它会给出错误,告诉它是来自该状态而且不允许..否则如果用户输入其他一些邮政编码,这应该没问题。我该怎么做?
So I want to write a validation that checks a zip code in a html input text. I want to check if a zip code is from a particular state then it will give an error, telling that it is from that state and it's not allowed.. otherwise if the user enters some other zipcode, this should be fine. How do I do that?
推荐答案
分配邮政编码的方式,每个州的邮政编码都在特定范围内。如果您忽略了当前并未使用这些范围中的所有邮政编码的事实,那么可以构建一个仅与特定状态的邮政编码匹配的正则表达式。 (有关范围,请参见)。
The way ZIP codes are assigned, each states' ZIP codes fall within a particular range. If you ignore the fact that not all ZIP codes in those ranges are currently in use, you could construct a regex that would match only ZIP codes for particular states. (see http://en.wikipedia.org/wiki/ZIP_code for the ranges).
我认为一个带有一些 if
语句的函数会更简单:
I think a function with some if
statements would be simpler though:
function isAllowedZIP(zip) {
if (zip >= 10000 && zip < 15000) // New York
return false;
if (zip >= 7000 && zip < 9000) // New Jersey
return false;
if (zip >= 90000 && zip < 96200) // California
return false;
return true;
}
这篇关于正则表达式,用于检查邮政编码文本是否不是来自特定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!