Page 1 of 1

address validation

Posted: Thu May 03, 2007 5:39 am
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))

Posted: Thu May 03, 2007 8:11 am
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 ;)

Posted: Thu May 03, 2007 11:09 am
by Benjamin
Yeah I can't imagine using regex for address validation.

Thanks but still not working

Posted: Thu May 03, 2007 2:35 pm
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?

Posted: Thu May 03, 2007 5:11 pm
by feyd
d11 forgot a "+" after the ending bracket.

Posted: Thu May 03, 2007 5:18 pm
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.

That Worked! Thanks so much!

Posted: Thu May 03, 2007 6:10 pm
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!