Hello and thanks in advance for taking the time to look at this. I have an issue validating a Mailing Address form field. The problems is when users enter an address that has two sets of numerals. For instance; "12345 ABC Street Suite 200" will result in a validation error. But if the user enters "12345 ABC Street Suite" it works correctly. So the issue is when the users enters a Numeral following a text string.

Here is my preg_match code

function checkStreetAddress($AddressStreet){
    if(!preg_match("^[a-zA-Z\.\-&', \/\\ ]{1,100}$^", $AddressStreet))
        return False;
    else
        return True;
}

Any ideas how to do this?

Recommended Answers

All 5 Replies

Hmmm if you want both to be valid, try:

'/^(\d{5})?([a-z.\-&\'",\/\\ ]+)(\d+)$/i'

This checks for:
Must start with a digit of length 5 (because of the {5} part) or no digits (because of the questionmark after the ")")
Must then be followed by one or more words
Must end in a number of any length, but with a minimum of one

The i after the last / sets the regex to be checked case-insensitive.
If you would like to change something in the first number, you could for example change {5} by {1,5} to define: a number with a length of 1 to 5.

You have not specified that digits are allowed. The reason the first validates is that the pattern specifies that it needs to end with what's between the [], but doesn't care what comes before that. Basically 01234567890123456789A is validated now. Changing the pattern correctly requires some sample data.

True that, hadn't even seen that the ^ was being used as delimiter ^^. Pritaeas, do you know which character you should use to match the start of a string if you use ^ as delimiter?

Thank you everyone. I just added 0-9 to my pattern and everything works great!

do you know which character you should use to match the start of a string if you use ^ as delimiter

It's still ^ for start of string, so you'd need to escape it with a backslash. Personally, I prefer to use the % as delimiter, I use that least often.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.