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?