PHP - Postcode check if it's covered
开发者_开发百科I am currently developing a website for an electrical company. They would like some sort of postcode check on there. It would somehow work like this:
- User enters postcode
- See if we cover it
- display results.
But I have never worked with postcodes before. How would I be able to check whether they cover it. I obviously need some sort of database listing the postcode or area they cover. But how would I also check if the postcode is valid.
The postcode lookup is obviously to see if the electrical company covers the user's area.
Thanks in advance.
I think instead of using a database to search your results, you would be better of looking at geo location, and using a 3rd party to calculate everything for you.
Google and Sony both provide Geo Location platforms
- Sony has: http://www.placeengine.com/en
- Google has: http://code.google.com/apis/maps/documentation/distancematrix/
I obviously need some sort of database listing the postcode or area they cover.
Yes. Then do a simple look up.
But how would I also check if the postcode is valid.
You would have to have a database listing all possible post codes and do a similar check.
I don't know which country you are in, but in most countries there is no programmatical way of determining wether a postcode is covered by a service or not.
Also no way of knowing wether it is valid or not without consulting a database that contains coverage.
There are databases available with postal codes, here is a dutch one: http://www.postcode.nl/index/269/1/0/overzicht-producten-en-diensten.html
You can validate the postal code using a regular expression.
The electrical company could surely give you a list of postcodes they do cover? Then it is a simple string matching from there....
I assume you are interested in the UK. To see if a postcode is valid grab the free CodePoint open data set from OS: http://www.ordnancesurvey.co.uk/oswebsite/products/code-point-open/
You could pull that into a DG and cross check user input. Just remember that this data is based on the Royal Mail PAF, so do not assume it is 100% accurate. Build a bit of flexibility/fault tolerance into your code.
If the client wants a specific radius covered, you coudl also use the OS data for distance calculations... and it is all FREE, as in both freedom and free beer :-)
As a first step you could check if the postcode is in a valid format: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation - this check will pre-filter input for you.
精彩评论