views:

649

answers:

7

I have a client which is shipping via UPS, and therefore cannot deliver to Post Office boxes. I would like to be able to validate customer address fields in order to prevent them from entering addresses which include a PO box. It would be best if this were implemented as a regex so that I could use a client-side regex validation control (ASP.NET).

I realize there's probably no way to get a 100% detection rate, I'm just looking for something that will work most of the time.

+3  A: 

This should get you started. Test to see if the Address field matches this regex.

"^P\.?\s?O\.?\sB[Oo][Xx]."

Translation to English: That's a P at the beginning of the line, followed by an optional period and space, followed by an O, followed by an optional period, followed by a space, followed by "Box", followed by anything else.

Bill the Lizard
One minor tweak to this regex: I'd start it of with ^\s* before the P so that a few spaces entered at the beginning of the field won't throw it off.
Dave Sherohman
I'm marking this as the accepted answer because it's the most relevant to the task at hand. The other approach of validating the address through the carrier is also very good, it's just not within our current timeline. Some combination of the two approaches is probably the best overall.
LockeCJ
@Dave: Sounds reasonable.
Bill the Lizard
@LockeCJ: I'd probably use a regex in Javascript on the client side, and use Jason Coco's solution on the server.
Bill the Lizard
+6  A: 

UPS also has tools that you can integrate to do this... that way you can verify an address exactly as to whether or not they will ship, what the cost would be, schedules, etc. I suggest visiting the UPS IT Solutions page for more information.

Jason Coco
I bet they would accept your shipping addresses and prevalidate them before picking up the goods, possibly offering a discount if you do. It's in their interest to have it be deliverable, too.
le dorfier
Unfortunately, I don't think this is feasable in the short term. First, the page that the customer enters their shipping address on is different from the one that contacts UPS. This is fine, but inconvenient. Second, the software allows USPS, FedEx, and UPS, which complicates things. Thanks anyway.
LockeCJ
+1  A: 

You might be better off putting a disclaimer on the page warning that you can not ship to post office boxes, opposed to validating the input.

More than likely if you do create a regex that catches most of the P.O. Box scenarios, there's a good chance it'll also catch things you weren't intending (i.e. a customer with a street name containing the letters 'p' 'o' and 'box')

John
It's fairly simple to create a regular expression that matches "P. O. Box 123", but doesn't match valid addresses.
Bill the Lizard
could do both as easily
le dorfier
Agreed. We were always intending to include a disclaimer on the page, but wanted to catch most of the cases where the user doesn't read, even though that almost never happens :)
LockeCJ
@Bill: Yep, in that scenario that's true; however, not everyone would type in the address exactly like that (with the periods)@Locke: Of course! who doesn't read the directions when filling out a form? :)
John
@John: The regex I submitted would match "P. O. Box", "PO Box", "P.O.Box", and many other variations, without false positives on real addresses.
Bill the Lizard
A: 

I'd start with a regex ala Lizard (but use the "ignore case" flag :)), test on historical data, then iterate as you see what invalid inclusions and exclusions you see in testing.

le dorfier
I wouldn't use the ignore case flag in this particular case. Testing could reveal that the "P" and the "O" should be capital, and the "box" should be either case. Using regex syntax is more flexible to change.
Bill the Lizard
A: 

Most shipping providers (for example FedEx) will validate the shipping address. For example, with FedEx web services, there is a call to validate a shipping address and get the estimated cost. This not only ensures that the address is not a PO Box, but also makes sure that the rest of the address is valid.

TAG
A: 

Regarding the OP's comment to Jason Coco's answer:

Since you're in a position to add regex validation to the shipping address, I assume that you have control of the application (i.e., you have the source and can modify it). If that's the case, then you should have the ability to, on reciept of the submitted data, check whether it is to be shipped via USPS, FedEx, or UPS and submit a request to the appropriate shipper-specific address validator, gaining all the benefits suggested in Jason's answer.

By making it shipper-specific, this would also allow you to avoid implementing one-size-fits-all rules, such as "no PO boxes because UPS doesn't deliver to them", even though the user can select non-UPS shippers who do deliver to PO boxes.

Dave Sherohman
Jason's answer is only unsuitable in the short term. I agree completely that a more general solution to the problem is needed. Once we are able to spend more time on this problem, that's definitely the direction we'll go. Also, as above, the flow of the app makes that approach less attractive.
LockeCJ
A: 

What if it doesn't start with "PO Box.." or "P.O. Box" ?

Example:

John Schmidt | Silver Valley PO Box 3901 | Whereswaldoville, SI. 78946

I used an onblur event for the address field to use a javascript function, indexOf, to recognize the input.toUpperCase "PO BOX" || "P.O" that is >= 0.

If either of these two searches are not found, the return is -1, otherwise, it will return the string's start position which will always be 0 or more.

This will ensure that lazy typing, 'po box,' 'p.o box,' and as well as 'p.o. box' will be recognized. I suppose you could add 'po. box' as well.

Anyway, the condition triggers an unobtrusive message to show that 'We can't ship to a PO Box address." It's a feature to not see it if it doesn't apply to you. Otherwise, for users who don't have js or css enabled, they'll just see the message. The only fail on this graceful degradation is if a user has css, but not js enabled (where they just won't see the message at all). I only came up with the solution today, but if I think of a better way, I'll come back to post it here.

Evan Smith