address validation

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
pendragon
Forum Newbie
Posts: 18
Joined: Mon Mar 19, 2007 3:25 pm

address validation

Post by pendragon »

Can anyone provide me with a fairly simple regular expression to validate street addresses that contain only digits, letters, spaces, dashes or periods? The following isn't working for me because it doesn't appear to catch the spaces in many address formats.

ereg("^[0-9a-zA-Z\s.-]+$", $address))
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

This seems incredibly presumptious. I might live at a street with a french name and therefore have an accent in it. Maybe I live at a street with an apostrophe (as in "My Street O' the Height" -- fairly common in England at least).

Code: Select all

preg_match('/^[a-zA-Z0-9\-\s\.]$/', $string);
What about commas? You're seriously heading for trouble if you are using this for address validation where customers give you their (correct) details ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah I can't imagine using regex for address validation.
pendragon
Forum Newbie
Posts: 18
Joined: Mon Mar 19, 2007 3:25 pm

Thanks but still not working

Post by pendragon »

Hi,

Thanks for the response, however preg_match('/^[a-zA-Z0-9\.\s\-]$/',$address) is still throwing an error
for an address formatted like 1234 Blueberry Court. I suspect it's not picking up the spaces between the words.

I also tried ereg("^[0-9]+[a-zA_Z]*\s+(([a-zA-Z]+.-)|([0-9]+))+\s+([0-9a-zA-Z]+?$", $address) to try to pick up additional variations on the address format but this isn't working either.

By the way, I have already verified that the information from the form is coming over correctly from the form page as 1234 Blueberry Court (with no odd characters or extra spaces in the entry). I used trim().

My code is as follows:

Code: Select all

if(ereg("^[0-9]+[a-zA-Z]*\s+(([a-zA-Z]+.-)|([0-9]+))+\s+([0-9a-zA-Z]+)?$", $address)){
          $valid_address = true;
      } else {
	      $valid_address = false;
		  $errors[] = 'ERROR: The address field is REQUIRED.  Your address can only contain digits, letters, spaces, dashes, or periods.';
      }
By the way, I wouldn't normally use this to validate an address as there are so many variations in address formats out there that you could end up turning away a user.

This is only one very small part of a larger programming exercise to learn pattern matching. It's the only part of my program that still produces the error message (I'm a newbie to PHP and haven't quite gotten the concept of pattern matching yet, it appears).

I intend to get back to the instructor with a question also, as there currently is an existing address in the database we're currently accessing in the format St. John's Place and using his validation requirements (only digits, letters, spaces, dashes, or periods), an address in this format couldn't be entered by a new user due to the apostrophe and would throw an error.

Can anyone see error in my code? I'm thinking that something like this preg_match('/^[a-zA-Z0-9\.\s\-]$/',$address) would probably need to be broken up into 2 or more separate blocks to catch an address like 1234 Blueberry Court. Maybe something like preg_match('/^[a-zA-Z0-9]+\s[a-zA-Z0-9\.\s\-]+\s[a-zA-Z0-9\.\s\-]+$/',$address)

Does this seem reasonable?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

d11 forgot a "+" after the ending bracket.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It's a best practice that you not use the ereg, rather use the PCRE functions like preg_match(). ereg is not binary safe, so it can be circumvented by things like null bytes.
pendragon
Forum Newbie
Posts: 18
Joined: Mon Mar 19, 2007 3:25 pm

That Worked! Thanks so much!

Post by pendragon »

Hi All,

Adding the + after the preg_match was the ticket! Should have caught that myself but didn't. Sometimes it pays to walk away from your code for a bit, do something else, and come back with fresh eyes.

Anyway, my last assignment of the year will be submitted and I'm free to begin studying for my final at the end of May. You've been a tremendous help. I spent my lunch hour re-reading the chapters on pattern matching - greedy versus non-greedy matches, etc. For some reason, pattern matching is taking a little extra time to sink in.

Thanks again!
Post Reply