views:

121

answers:

2

I want to validate that zip code entered by user is valid or not.

for example user entered 009876654 and it is not valid then an error message should be given.

I know i can do it using javascript regulr expression or using ajax-zip-code-database

But i don't want any of the above. i need some plugin sort of thing which send request to some online application to check wheather it is valid or not.I want this because i don't want to take care if in future there is change in the zip-codes or new zip-codes get added.

P.S. :- I don't want to use javascript or using ajax-zip-code-database

A: 

Assuming your application is commercially compatible with their terms of use, I'm wondering if you can use Google's gecoder service to lookup a zip/postal code and then to check the results to see if it exists. I would assume if you get back a postcode and a sane lat, lng pair you could conclude the zipcode is real.

The code below (admittedly using the now deprecated V2 API shows one approach for a US-centric search). The advantage is that it's the end-user and Google compute resources and bandwidth that are used to do the validation.

I don't know if this a bit heavy for your purposes although I've found Google's gecoder to be blindingly fast.

gecoder = new GClientGeocoder();

geocoder.getLocations(zipcode, function(response) {
    if (response && response.Status.code === 200) {
        var places = response.Placemark;
        for (var p in places) {
            if (places[p].AddressDetails.Country.CountryNameCode === 'US') {
                // lat => places[p].Point.coordinates[1],
                // lng => places[p].Point.coordinates[0],
                // zip => places[p].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber
            }
        }
    }
});
bjg
+1  A: 

There is a web service at webservicex that can give you XML results from a GET or even a POST call. I've never used it but it seems to be what you're looking for.

Non-existent zip codes return an empty data set

wget http://www.webservicex.net/uszip.asm /GetInfoByZIP?USZip=60001

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <Table>
  <CITY>Alden</CITY>
  <STATE>IL</STATE>
  <ZIP>60001</ZIP>
  <AREA_CODE>815</AREA_CODE>
  <TIME_ZONE>C</TIME_ZONE>
</Table>
</NewDataSet>
Paul Rubel