SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to validate a mailing address

    I have a need to validate mailing addresses.

    I have the following code created. It works, but i'm wondering if anyone else has had to validate a mailing address and has a different/better solution

    Code:
    function validateAddress() {
    	$url = 'http://maps.google.com/maps/geo?q=' . urlencode($this->address . ', ' . $this->city . ', ' . $this->country) . '&output=csv&key=' . $this->key;
    	$this->data = file_get_contents($url);
    	$this->data = explode(',', $this->data);
    	return ($this->data[0] == 200) ? array('lat' => $this->data[2], 'long' => $this->data[3]) : '';
    }

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Haven't done that, but I wouldn't trust it so much. Google Maps doesn't cover ALL of the map by far. If user Joe comes from a Joomboomboom village somewhere in Africa, it would fail validation even if it really exists.
    Saul

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya trying it out on my database it's not working too well anyway. it's validating things like '12, london, UK'.

    Our free sample warehouse is complaining about the address going through. Guess they will just have to deal with it

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you'd need a database of addresses or something of that sort, and then it's not likely it would contain ALL addresses.

    A regular validation making sure it at least meets the format would do for today. Maybe some time in year 2075 (I'll have my book published by then btw).

    The rest, yes they will have to deal with, that's what they get paid for!!
    Saul

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You need to define scopes based on region as each country will have its own way of formatting and dealing with addresses. Having one form of validation for the whole world requires you over flow it was data and checks.

    I personally like to keep things as simple as possible and breaking the validation down pre-region will aid in that. And you can focus on the actually places you will and can ship to.

    Instead of using google maps to validate if the address is real go to one of the internal shipping companys and see if the provided a means to check an address.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,449
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    If you're in the US you can use the USPS API to help validate addresses.

  7. #7
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We ship to many different countries not just the US. But each site will only ship to its respective country. We have many different country specific sites.

    Quote Originally Posted by logic_earth View Post
    You need to define scopes based on region as each country will have its own way of formatting and dealing with addresses. Having one form of validation for the whole world requires you over flow it was data and checks.
    Seems like even developing a per-country validation would be tough to accomplish as well given all the different ways of formatting an address.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    Seems like even developing a per-country validation would be tough to accomplish as well given all the different ways of formatting an address.
    As long as you follow that countries postal code standard then you should be fine. If you revive input from users that don't follow you can try and guess or reject the address and have the user enter it again.

    If you try and guess add an extra step to tell the user about the change and get them to confirm its right.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •