ryryan
ryryan

Reputation: 3928

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:

  1. User enters postcode
  2. See if we cover it
  3. 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.

Upvotes: 5

Views: 2376

Answers (5)

Adam
Adam

Reputation: 81

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.

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

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

Upvotes: 4

TJHeuvel
TJHeuvel

Reputation: 12608

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.

Upvotes: 0

gnur
gnur

Reputation: 4733

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.

Upvotes: 0

Quentin
Quentin

Reputation: 943560

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.

Upvotes: 0

Related Questions