PHP - input form address check

Posted on 2007-07-24
Last Modified: 2011-10-03
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?
Question by:innercproductions
    LVL 14

    Expert Comment

    by:Aamir Saeed
    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.

    Author Comment

    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.
    LVL 1

    Expert Comment

    I'm not american, please can you post an example of the address in the correct format? thanks.

    Author Comment

    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.

    LVL 1

    Accepted Solution

    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.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now