Right guys i need some help and I’ve been thinking about this for some time and cant come up with a way round the issue. I have a form which consumers fill in, they need to enter their UK Post Code. Now i need to send that info onto our client but they require the postcode be sent in 2 strings. For example:
Postcode=WA28PR
SPLIT TO:
Postcode1=WA2
Postcode2=8PR
Now there’s different formats a UK Post Code can be:
Format Example Coverage
A9 9AA M1 1AA B, E, G, L, M, N, S, W postcode areas
A99 9AA B33 8TH
AA9 9AA CR2 6XH All postcode areas except B, E, G, L, M, N, S, W, WC
AA99 9AA DN55 1PT
A9A 9AA W1A 1HQ E1W, N1C, N1P, W1 postcode districts (high density areas where codes ran out)
AA9A 9AA EC1A 1BB WC postcode area; EC1–EC4, NW1W, SE1P, SW1 postcode districts (high density areas where codes ran out)
As you can see the different formats, now the issue i have is how do i go about working out where I should split the postcode value? The obvious thing for me to do would be to ask people to enter their first part and second part of their postcode in 2 different fields but I don’t really want to do it that way as it may confuse consumers.
The last 3 characters are always the final part of the post code.
Lop them off, and what remains should be the first part – does that pass all tests?
W1A1AA
So you might be sent a valid looking string – but you (and your end client) might be more interested in trying to validate that you have been sent a real postcode before saving:
Try using one of these [google]postcode api uk[/google]
How do i go about splitting the value of postcode into postcode1 and postcode2 with postcode2 having the last 3 values and postcode1 having the remaining?
I know how to do it if theres ---- in between them as id just use this:
Thanks for the help, sorry for not responding sooner i never got an emailing there was a new reply. I’ve managed to code what i need it do to but i have an issue, below is my code:
…and the result must each equal array(“WA2”, “8PR”);
You need to create a function which
a) removes all of the spaces
b) removes anything which is not a permitted letter or a permitted number
c) uppercases what is left
d) checks the length is valid
// else return false
e) takes of the last 3 chars
f) isolates the remaining chars
g) returns an array with the previous 2 values
Why don’t you give it your best shot at running some code against that test array and see how close you can get, then ask for help if you get stuck.
here’s a start at a) we dont validate vs the final array for the time being, just against the expected result: “WA28PR”
So, what’s next what do you add to the function to get more passes?
What can you add to the original array to make your tests more lifelike?
You could create a new array of values which should always fail (return false).
That is one way of thinking about solving your own problems.
Step 1 is actually defining your problem.
Postcodes arnt simply 6 character strings, unfortunately.
For Cups’ sake, i’m going to define the postcode in a Regex pattern. This will help him narrow down what you need to do.
/[A-Z][A-Z]?[0-9][0-9A-Z]?\s[0-9][A-Z]{2}/
(Hint: This regex (slightly altered) could be used to simplify your problem greatly…)