/    Sign up×
Community /Pin to ProfileBookmark

Simple postcode checker. User types in postcode, javascript to validate from list?

Hi There

I’ve done some googling on this issue and I think I’ve worked out how to do this, I’d just like to know if the solution I have is correct, and also if somebody could point me in the right direction for writing this out.

A client I am working for has requested a Postcode checker, to see if the user is eligible to recieve a service. I have a list of many postcodes. The user would type in their postcode, hit enter or submit, and it will flash up if the postcode they have entered matches one in the list.

I am thinking I’m going to need to write a regular expression to achieve this task, but I have no idea where to begin, and then I’m not sure how I can work this into a javascript code to get the validation working on a text field. Can anyone point me in the right direction?

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@rootOct 24.2017 — You can get a script to evaluate how far someone is from a certain post code.

You could try http://lmgtfy.com/?q=how+to+calculate+distance+from+a+postcode and look for databases as well.
Copy linkTweet thisAlerts:
@sambrownauthorOct 24.2017 — You can get a script to evaluate how far someone is from a certain post code.

You could try http://lmgtfy.com/?q=how+to+calculate+distance+from+a+postcode and look for databases as well.[/QUOTE]


Thanks for your response, but it seems you haven’t understood my question.

I’m asking for suggestions for a solution for the following:

User types in a postcode via an input field

Javascript checks from a lift of postcodes for a match

If matches, div is shown.

I am not looking to calculate any distance between postcodes. Many thanks.
Copy linkTweet thisAlerts:
@rootOct 25.2017 — Post codes do not cover regular areas, for example in my locality, on my side of the road it is a post code AB3 1YZ whereas on the other side of the road it is AB1 4MN but we are still the same street, so postcodes are not an good way of allotting service cover. All the companies I have worked for that provided service cover, including one company where I was responsible for a department and several engineers, service cover was based on distance from the service engineers home, if it was too far then the service request was then pushed to a contractor who was calculated to be nearest to the client.

Yes we used post codes in that instance but only as the location starting point and a distance was calculated as a maximum distance to keep within driving times.

So in reality, its not a misunderstanding or not understanding your needs, I was pointing you to the fact that you can easily get information on postcodes and various scripting, my response was one possible from many.
Copy linkTweet thisAlerts:
@SempervivumOct 25.2017 —  I have a list of many postcodes.[/QUOTE]Where are these? In a database? In a textfile? In a javascript array?
Copy linkTweet thisAlerts:
@sambrownauthorOct 25.2017 — Where are these? In a database? In a textfile? In a javascript array?[/QUOTE]

I have been provided a list of postcodes from the client that they wish to include in the form of a text file. The list is rather long, so I assume it might be better to store them in a database, or come up with a regex for them. I’m not too sure what the best method is going forward.
Copy linkTweet thisAlerts:
@sambrownauthorOct 25.2017 — Post codes do not cover regular areas, for example in my locality, on my side of the road it is a post code AB3 1YZ whereas on the other side of the road it is AB1 4MN but we are still the same street, so postcodes are not an good way of allotting service cover. All the companies I have worked for that provided service cover, including one company where I was responsible for a department and several engineers, service cover was based on distance from the service engineers home, if it was too far then the service request was then pushed to a contractor who was calculated to be nearest to the client.

Yes we used post codes in that instance but only as the location starting point and a distance was calculated as a maximum distance to keep within driving times.

So in reality, its not a misunderstanding or not understanding your needs, I was pointing you to the fact that you can easily get information on postcodes and various scripting, my response was one possible from many.[/QUOTE]


Apologies for the misunderstanding. In this instance, the client has provided the list of postcodes for us to use and I’m simply looking for a way of getting them into a validity checker.
Copy linkTweet thisAlerts:
@SempervivumOct 25.2017 — Simplest way: Keep the textfile, load it by PHP into an array and check if the postcode is in it.

More sophisticated way: Load the textfile into a database.

Please post some sample lines of the file.

I'm sceptical if this task can be done by a regex. The postcodes would have to match a fixed pattern.
Copy linkTweet thisAlerts:
@jedaisoulOct 25.2017 — It is important to get the terminology right. In the UK, there are literally millions of Postcodes. Often you are not interested in the precise postcode, so they are grouped into Sectors, which are grouped into Districts. E.g.

Full postcode: AB1 1AA

Sector: AB1 1

District: AB1

New and amended postcodes are added monthly so require a lot of maintenance. New sectors are added or amended occasionally. Districts are very rarely added or amended, so require the least maintenance. However, districts are often too large an area to be useful. So it is commonplace to use the sectors. A sector is generally part, or all, of a street and may compose 8 to 12 postcodes (or thereabouts).

Postcodes are also sub-divided into the "outward" code (the district), and the "inward" code (the rest). The space between the outward and inward postcode is an integral part of the coding.

So has the client given you a list of full postcodes, or sectors or districts?
Copy linkTweet thisAlerts:
@sambrownauthorOct 25.2017 — It is important to get the terminology right. In the UK, there are literally millions of Postcodes. Often you are not interested in the precise postcode, so they are grouped into Sectors, which are grouped into Districts. E.g.

Full postcode: AB1 1AA

Sector: AB1 1

District: AB1

New and amended postcodes are added monthly so require a lot of maintenance. New sectors are added or amended occasionally. Districts are very rarely added or amended, so require the least maintenance. However, districts are often too large an area to be useful. So it is commonplace to use the sectors. A sector is generally part, or all, of a street and may compose 8 to 12 postcodes (or thereabouts).

Postcodes are also sub-divided into the "outward" code (the district), and the "inward" code (the rest). The space between the outward and inward postcode is an integral part of the coding.

So has the client given you a list of full postcodes, or sectors or districts?[/QUOTE]


Many thanks for your response. In all honesty I was expecting to be supplied just the districts, but instead I have been supplied a full list of postcodes. I'm thinking it isn't necessary to use the full list, and just validate by the sectors. This is why I was thinking a regex might be suitable.
Copy linkTweet thisAlerts:
@rootOct 25.2017 — How large is the file? as in kB and quantity of entries?

Whilst using a FlatFile may be appealing, it will have an impact on the server and the site compared to a database.
Copy linkTweet thisAlerts:
@JMRKEROct 25.2017 — Many thanks for your response. In all honesty I was expecting to be supplied just the districts, but instead I have been supplied a full list of postcodes. I'm thinking it isn't necessary to use the full list, and just validate by the sectors. This is why I was thinking a regex might be suitable.[/QUOTE]

Your 'full list of postcode' appears to be an alphabetized list of 6 or 7 character text.

I would suggest a binary search for a return of found/not-found.

Example:

1. Pick middle entry of file.

2. Check if user entry is higher or lower.

3. If User < File entry, repeat 1-2 with new file limits of 1 to (filesize/2)-1

If User > File entry, repeat 1-2 with new file limits of (filesize/2)+1 to filesize

4. If User = File entry, postal code has been matched.

5. If start of comparison is higher than end of comparison, entry does not match any in list.

Note if file size is 10000 entries, there should be no more than 14 checks (2 ^ 14 = 16384).

There are many sources for a binary search algorithm available.
Copy linkTweet thisAlerts:
@rootOct 26.2017 — You would be best writing a script to read all the rows of the file and insert each one in to a database.

When I did it, it was a royal pain because the file had many errors in it from the person who supplied it, once I had corrected the problems, there were a few problems from the script and a step I should have taken and that was to escape certain characters like apostrophes in the street name or in cases where a postcode was a property or building with a name.

So preparing the text file is important and it might take some working out, but more quicker than inserting every postcode by hand.
Copy linkTweet thisAlerts:
@rootDec 19.2018 — New postal areas are added all the time, some areas are removed when they are buildings that are levelled and turned in to public recreation space, some are extensions to existing codes... Many reasons to keep your postal DB up to date, NOTHING is fixed...
×

Success!

Help @sambrown spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...