Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

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.

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.

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.

Unfortunately, UPS's online software allows P.O. Boxes to go through, but will choke on them once they're in the shipping channel. In our case, our cart abandonment rate went up when we tried to gracefully prevent P.O. Boxes. We found it much more cost effective to leave it alone, accept the sale, bring it to the attention of customer service, and let them resolve it. Of course, if you get a high incidence of P.O Boxes, this may not be the case for you.

From the original question:

I realize there's probably no way to get a 100% detection rate, ...

Actually, there is.

If the address is first validated and standardized to the USPS format using an address validation tool (the company I work for makes YAddress), then all P.O. Box addresses without exception will be spelled "PO BOX ...". After that you can string-compare them for an exact match with 100% accuracy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top