Jump to content

Address validation (Excluding PO Box addresses)


cyanoxide

Recommended Posts

Hi there, we currently process orders with two different carries, both of which will not deliver to PO Box addresses. Our solution to this was pretty simple. We added a red warning above the Street address field that stated that PO Boxes were not valid and inputting one will cause delays as we will have to contact them to gather a different address.

 

This may of had some effect on reducing the amount of people that put PO Box addresses into the address field but we still get a significant amount of them come through anyway so I'm looking for a more effective solution.

 

I've searched the forums and stackoverflow and found that the best method of excluding them is with a regular expression. I found this on the forums https://regex101.com/r/nC4lZ2/4 by a user called Ornot which looks to be exactly what I need but I'm unsure as to what I need to do to activate it. I've tried using the pattern attribute for my input field and passing the expression in there hoping it was as simple as that but unfortunately that was not the case.

 

I then looked to stackoverflow for javascript/jquery solutions but none of them seemed to work either so I'm not sure how I should be going about doing this.

 

Any help on the matter would be greatly appreciated.

Link to comment
Share on other sites

For anyone looking to do this, this is the solution I finally came up with.

<input type="text" class="form-control" name="address1" id="address1" value="{if isset($smarty.post.address1)}{$smarty.post.address1}{/if}" title="Please enter a valid street address, we do not except P.O. Box addresses" pattern="^(?! *(#\d+|([bB][oO][Xx]|[Bb][Ii][Nn])[-. /\\]?\d+|.*[pP][ .]? ?[Oo0][-. /\\]? *-?(([bB][oO][Xx]|[Bb][Ii][Nn])|b|(#|[Nn][Uu][Mm])?\d+)|[Pp]([Oo][Ss][Tt])? *([Oo]([Ff]{2}([Ii][Cc][Ee])?)?)? *(([bB][oO][Xx]|[Bb][Ii][Nn])|? *\d+|[Pp] *-?/?[Oo]? *-?[bB][oO][Xx]|[Pp][Oo][Ss][Tt] [Oo][Ff][Ff][Ii][Cc][Ee] ([bB][oO][Xx]|[Bb][Ii][Nn])|(([bB][oO][Xx]|[Bb][Ii][Nn])|[Bb]) *([Nn][Uu][Mm][Bb][Ee][Rr]|[Nn][Uu][Mm]|#)? *\d+|([Nn][Uu][Mm][Bb][Ee][Rr]|[Nn][Uu][Mm]|#) *\d+)).*"/>

Open up authentication.tpl and find the input fields with the id of "address1" (there should be two of them within the file) and replace them with the code provided above. From what I can tell that should be enough to validate against PO Box addresses inputted into the fiels. No additional code is required.

 

Below you will find the additions and what they do:

title="Please enter a valid street address, we do not except P.O. Box addresses" 

When you enter something into the field that doesn't match the validation criteria you will receive a message telling you to make sure your content is inputted in the correct format but by default it doesn't tell you what the correct format should be. Adding a title attribute will allow you to add content to the validation message that pops up giving the user an indication of why the form will not submit.

pattern="^(?! *(#\d+|([bB][oO][Xx]|[Bb][Ii][Nn])[-. /\\]?\d+|.*[pP][ .]? ?[Oo0][-. /\\]? *-?(([bB][oO][Xx]|[Bb][Ii][Nn])|b|(#|[Nn][Uu][Mm])?\d+)|[Pp]([Oo][Ss][Tt])? *([Oo]([Ff]{2}([Ii][Cc][Ee])?)?)? *(([bB][oO][Xx]|[Bb][Ii][Nn])|? *\d+|[Pp] *-?/?[Oo]? *-?[bB][oO][Xx]|[Pp][Oo][Ss][Tt] [Oo][Ff][Ff][Ii][Cc][Ee] ([bB][oO][Xx]|[Bb][Ii][Nn])|(([bB][oO][Xx]|[Bb][Ii][Nn])|[Bb]) *([Nn][Uu][Mm][Bb][Ee][Rr]|[Nn][Uu][Mm]|#)? *\d+|([Nn][Uu][Mm][Bb][Ee][Rr]|[Nn][Uu][Mm]|#) *\d+)).*"

The pattern attribute is what tells the input field what is valid and what is not. This pattern can be used to exclude PO boxes is some of the main text formats that people might us (POBOX, P.O box, po bin, etc). Adding this to any text based input field will allow you to make that form validate against PO boxes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...