PO Box正则表达式验证

问题描述 投票:11回答:10

这是我的代码,但我无法触发警报。

$(document).ready( function (){
    $("[id*='txtAddress1S']").blur(function() {
        var pattern = new RegExp('\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b');
        if ($("[id*='txtAddress1S']").val().match(pattern)) {
            alert('We are unable to ship to a Post Office Box.\nPlease provide a different shipping address.'); 
        }

    });
});

提前致谢!

javascript jquery regex
10个回答
15
投票

在javascript中,你必须逃避你的斜线:

var pattern = new RegExp('\\b[P|p]*(OST|ost)*\\.*\\s*[O|o|0]*(ffice|FFICE)*\\.*\\s*[B|b][O|o|0][X|x]\\b');

此外,您可以通过使用不区分大小写的匹配来减少您的模式:

var pattern = new RegExp('\\b[p]*(ost)*\\.*\\s*[o|0]*(ffice)*\\.*\\s*b[o|0]x\\b', 'i');

注意:您的正则表达式也匹配以下地址:

  • 123穷人路
  • 123 Harpo Box Street

我建议还检查字符串中的数字。也许来自previous answer的这种模式会有所帮助:

var pattern = new RegExp('[PO.]*\\s?B(ox)?.*\\d+', 'i');

(它不会与拼写出的“邮局”或数字替换相匹配..但它是一个开始。)


0
投票

我发现了一种适用于最逼真的邮局地址的模式。在与@ jonathan-marzullo的answer for Rural Route Post 1(这不是邮政地址)获得假阳性之后,也不得不陷入困境。

我的模式是

pattern = P([.]?(O|0)[.]?|ost|ostal).((O|0)ffice.)?Box{1}\b/i

符合以下情况:

PO Box
P.O. Box
PO. Box
po box
P.o box
po box
p.o. box
post office box
P.O. Box
PO. Box
PO Box
Postal Box
Post Office Box
P.O Box
post office box 1
postal office box 1
postal box 1

与以下情况不符:

post office b 1
post off box 1
post off b 1
post o box 1
post o b 1
p office box 1
p office b 1
p off box 1
p off b 1
p o box 1
p-o-b-1
p.o.b.1
POB
pob #1
pob num1
pob number1
foo pob1
box #1
po 1
pb 1
Expo Blvd
Rural Route Box 1
Army Post 1
postal office 1
poop box
pony box
POBox
P O Box
PO
po bo
Pobox
Post

33
投票

我尝试了在互联网上找到的几个PO Box RegExp模式,包括在Stack Overflow上发布的模式,它们都没有通过我们的测试要求。因此,我在下面发布了我们的RegExp和我们的测试集:

var poBox = /^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)/i;

var matches = [ //"box" can be substituted for "bin" 
    "#123", 
    "Box 123", 
    "Box-122", 
    "Box122", 
    "HC73 P.O. Box 217", 
    "P O Box125", 
    "P. O. Box", 
    "P.O 123", 
    "P.O. Box 123", 
    "P.O. Box", 
    "P.O.B 123",
    "P.O.B. 123", 
    "P.O.B.", 
    "P0 Box", 
    "PO 123", 
    "PO Box N", 
    "PO Box", 
    "PO-Box", 
    "POB 123", 
    "POB", 
    "POBOX123",
    "Po Box", 
    "Post 123", 
    "Post Box 123", 
    "Post Office Box 123", 
    "Post Office Box", 
    "box #123", 
    "box 122", 
    "box 123", 
    "number 123", 
    "p box", 
    "p-o box", 
    "p-o-box", 
    "p.o box", 
    "p.o. box", 
    "p.o.-box", 
    "p.o.b. #123", 
    "p.o.b.", 
    "p/o box", 
    "po #123", 
    "po box 123", 
    "po box", 
    "po num123", 
    "po-box", 
    "pobox", 
    "pobox123", 
    "post office box" 
];

var nonMatches = [ 
    "The Postal Road", 
    "Box Hill", 
    "123 Some Street", 
    "Controller's Office", 
    "pollo St.", 
    "123 box canyon rd", 
    "777 Post Oak Blvd", 
    "PSC 477 Box 396", 
    "RR 1 Box 1020" 
]; 

5
投票

使用Javascript更容易使用正则表达式文字,如下所示:

var pattern = /\b(?:p\.?\s*o\.?|post\s+office)\s+box\b/i;

(不需要反斜杠!)


1
投票

这是我用空间和不区分大小写的方法:

http://regexr.com/3cc2q

var pattern = /\bP(ost|ostal)?([ \.]*(O|0)(ffice)?)?([ \.]*Box)?\b/i;
  • 失败 - po box
  • 失败 - p.o.框
  • 失败 - 波波
  • 失败 - 邮政信箱
  • 失败 - P.O.框
  • 失败 - PO。框
  • 失败 - 邮政信箱
  • 失败 - POBox
  • 失败 - P O Box
  • 失败 - P.O Box
  • 文件 -
  • 失败 - 邮政信箱
  • 失败 - 邮政信箱
  • 传球 - 大便箱
  • 传球 - 小马箱

0
投票

这个对我们来说非常好。 (php preg_match)

$pattern = '!p(ost)?\.?\s*o(ffice)?\.?(box|\s|$)!i';

0
投票

它对我有用,

var poRegex = /\bP(ost|ostal)?([ \.]*O(ffice)?)?([ \.]*Box)?\b/i;

0
投票

这里有一个匹配'POB','PO Box'和'Post Office Box'。图案:

\\b[PO.|Post\\sOffice]*\\s?B(ox)?.*\\d+\\b

这是对@drudge解决方案的修改。


0
投票

上面提供的正则表达式是接受正确的邮政信箱。修改后的Regex不接受的是:

var pattern = /^ *(?!(#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#)|(?![a-zA-Z0-9\x20'#,]) *\d+)/i;

0
投票

在使用@ Dathan的answer生产几个月后,我们遇到了误报PO盒。这个简化版本确保模式后面跟一个数字,所以不会匹配像“Expo Blvd”这样的东西。它还允许在address2字段中常见的“#123”和“B1”等公寓/单元/套房号码。你可以玩它并在这里添加你自己的测试用例:https://regex101.com/r/7ZUQFl/2

const re = /^\s*(.*((p|post)[-.\s]*(o|off|office)[-.\s]*(b|box|bin)[-.\s]*)|.*((p|post)[-.\s]*(o|off|office)[-.\s]*)|.*((p|post)[-.\s]*(b|box|bin)[-.\s]*)|(box|bin)[-.\s]*)(#|n|num|number)?\s*\d+/i;
const matches = [
  "post office box 1",
  "post office b 1",
  "post off box 1",
  "post off b 1",
  "post o box 1",
  "post o b 1",
  "p office box 1",
  "p office b 1",
  "p off box 1",
  "p off b 1",
  "p o box 1",
  "p-o-b-1",
  "p.o.b.1",
  "POB1",
  "pob #1",
  "pob num1",
  "pob number1",
  "foo pob1",
  "box #1",
  "po 1",
  "pb 1"
];
const nonMatches = [ 
  "Expo Blvd",
  "Rural Route Box 1",
  "Army Post 1",
  "B1",
  "#1",
  "N1",
  "Number 1",
  "Num 1"
];
© www.soinside.com 2019 - 2024. All rights reserved.