PO Box Validation

后端 未结 3 1215
别那么骄傲
别那么骄傲 2021-02-15 16:44

Looking to validate PO Box but wanted to know if such validation existed. I have the Address field split into Address 1 and Address 2 (Where such PO, Apt, Suite info would go)

相关标签:
3条回答
  • 2021-02-15 16:52

    Let's go through it...

    /         # Beginning of the regex
    ^         # Beginning of the string
    \s*       # (Any whitespace)
    ((
      P       # Matches your P
      (OST)?  # Matches your ost
      .?      # Matches the space
      \s*     # (Any whitespace)
      O       # Expects an O - you don't have one. Regex failed.
    
    0 讨论(0)
  • 2021-02-15 17:07

    As of now with your regex, the 'O' in 'OFFICE' is required. Try ^\s*((P(OST)?.?\s*(O(FF(ICE)?))?.?\s+(B(IN|OX))?)|B(IN|OX)) instead (grouping the 'O' in a conditional match).

    EDIT: That should be /^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i instead. BTW, http://rubular.com/ is a pretty good regular expression testing engine. Always nice to know of new tools :)

    0 讨论(0)
  • 2021-02-15 17:09

    This one works better, as it removes the unneeded groups in the match set and just returns the whole match.

    Skips Post 123:

    /^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)+)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i
    

    Doesn't Skip Post 123:

    /^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)?)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i
    

    Remove the \d+ at the end to skip the number requirement.

    0 讨论(0)
提交回复
热议问题