Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PO Box Validation

Tags:

regex

php

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)

Example:

Address 1: 123 Main Street
Address 2: Suite 100
City: Any Town      
State: Any State
Zip: Any Zip

PO Box (Can sub BIN for BOX as well) Examples:

  • PO Box 123
  • P.O. Box 123
  • PO 123
  • Post Office Box 123
  • P.O 123
  • Box 123
  • 123

  • 123
  • POB 123
  • P.O.B 123
  • P.O.B. 123
  • Post 123
  • Post Box 123

(I know there are probably more I need to validate for but this is what I could think of, feel free to add or correct)

I know a RegEx would be best for this and I've seen the other questions on Stack #1, #2

Using the RegEx from the other question I get good results but it misses some I think it should catch

$arr = array (
    'PO Box 123',
    'P.O. Box 123',
    'PO 123',
    'Post Office Box 123',
    'P.O 123',
    'Box 123',
    '#123',         // no match
    '123',          // no match
    'POB 123',
    'P.O.B 123',    // no match
    'P.O.B. 123',   // no match
    'Post 123',     // no match
    'Post Box 123'  // no match
);

foreach($arr as $po) {
    if(preg_match("/^\s*((P(OST)?.?\s*O(FF(ICE)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", $po)) {
        echo "A match was found: $po\n";
    } else {
        echo "A match was not found: |$po| \n";
    }
}

Why is it not catching the last two values in the array?

like image 448
Phill Pafford Avatar asked Mar 01 '11 19:03

Phill Pafford


People also ask

How do you validate a PO box?

Post office address verification works to validate a P.O. box address or street address. In most locations, you are asked to use a touch screen to accept the address shown. It will be the address you provide, correctly formatted according to USPS rules.

What does PO Box address mean?

A post office box (commonly abbreviated as P.O. box, or also known as a postal box) is a uniquely addressable lockable box located on the premises of a post office.


3 Answers

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 :)

like image 67
William Avatar answered Oct 15 '22 21:10

William


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.
like image 2
EboMike Avatar answered Oct 15 '22 21:10

EboMike


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.

like image 2
Torgo Avatar answered Oct 15 '22 21:10

Torgo