views:

74

answers:

5

I have a job board where a user submits his location, via Google-Maps API, which plots the job on a map.

The problem is, because it is a location based job board, I want to make the most broad input allowable a ZIP code (so users can either enter a ZIP code or address). Is there anyway to either parse the input to determine whether it's a zip code or address - or does Google Maps API return anything that would let me know how broad the input the user supplied was?

A: 

ZIP codes in the US are usually written either like:

  • 90210
  • 90210-5555
  • 902105555

If it's not one of those, try interpreting it as an address?

John at CashCommons
+1  A: 

An address will always have at least one space. so do a simple match on that.

if (preg_match("/ /", $input)
{
    //   its an addresss!
}

Also an address will never be all digits/dashes

if (preg_match("/^[0-9\-]$/"))
{
    // its a zipcode!
}
Byron Whitlock
+2  A: 

I found a jquery plugin parse addresses VIA google maps, that you might want to try. From the doco:

$("#submit").click(function(){

  // use parseaddress plugin on an element, send response to callback function

  $("#addressinput").parseaddress(callback);

});

var callback = function(cleanaddress) {
  console.log(cleanaddress['street']);
  console.log(cleanaddress['city']);
  console.log(cleanaddress['state']);
  console.log(cleanaddress['country']);
  console.log(cleanaddress['zip']);
  console.log(cleanaddress['lat']);
  console.log(cleanaddress['lon']);
}
snoopy
I'd take a look at the source for that function so you don't have to do 2 round trips to google maps. (one to validate address, another to get the map)
Byron Whitlock
+1  A: 

There are many SO postings discussing address parsing; search for "parse address zip" and similar phrases. There are also many tools already written specifically for address parsing. Use them, because addresses are complicated with many considerations that take a lot of time to learn about. Let the existing tools do the work for you.

joe snyder
A: 

The way I do this is to basically check if it a number and if so assume it is a zip code, otherwise assume it is an address. After all, you couldn't possibly have an address that was just a number and nothing else.

Craig