PHP - input form address check

I have 1 box for a site visitor to add their address. (assuming USA address)

What are some suggestions on check the address at the PHP level to make sure it's in the right format?
Any suggestions?
Who is Participating?
Of course I also think you should use multiple fields to guide the user to insert correctly the data; using a unique field will leave the user more free with a cleaner form layout, but a little less shure of how to insert correctly his data. You should provide at least an example of an address correctly written. If you want to guess the input in an unique field I see only one way, progressively break the input data using the number, position and type of each single item we get to guess if it conforms with what we are expecting.

- First of all break the input at the end of each line ('\r', '\n' characters or both), so you expect to get tree strings (one for each row, if they are not three you can already tell the user he did not insert the data correctly)

- about the first row just break it at the space characters, the first item should be the number, the remaining are the 1 or more words forming the name of the street

- about the second row I think you could keep it as a whole string

- about the third row just break it at the commas and keep all the words before the breaking point as the name of the city, then break the remaining at the speces, you aspect to get two items, the state and the zip code.

- at this point if you want you can further validate the data you have extracted like the existence of  zip code and state

Of course this is a little basic and you need to think to special cases (what if a user writes a comma in the city name?, It seems to me that the second row of your example is optional so you have to add flexibility also for that etc. ) I belive a system like this can give you better results of what you expect if you carefully consider the most common possibilities of input.
Aamir SaeedCommented:
i think rather than checking the format, you can format address yourself. for example, give the user different input fields to enter state, city, zip and other address into and then format it in your way.
innercproductionsAuthor Commented:
I, of course, thought of that. It would be ideal.
But under the circumstances, I was looking for any ideas for a 1 box input situation.
I'm not american, please can you post an example of the address in the correct format? thanks.
innercproductionsAuthor Commented:
An example of a basic USA address format would be:

3535 Street Address
Apt. 54
City, ST  99999                   (City, State  Zip Code)

But I'm probably going to have to lean towards separate input boxes for each field.
Though I was requested not to do it that way by my client.

If someone can help me, great.  I will leave this thread open for a while.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.