If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Little problem with this form

I'm fairly new to JavaScript so Iím still trying to get my head around it and stuff, but Iíve managed to come up with a basic form with checks in place to validate the form when it is submitted. Iíve managed to get everything working except for one of the input fields which are the UK post code field. Iíve found an expression for the UK post code but Iím having difficulty with implementing it correctly so it displays the alert when the user enters there Postcode in an incorrect format. Iíve placed all of the forms checks inside one function which validates the form, if somebody could help me out with this post code issue that would be great!

I'm ignorant of what constitutes a valid UK postal code. However, the regular expression in your post has an obvious logical flaw at the end:

Code:

[a-zA-z][a-zA-z]

I'm almost certain these were meant to mean all upper and lower case letters like the others:

Code:

[a-zA-Z]

so that could esily be the problem or at least a part of it.

Also, [0-9] means the same as \d. And letters may be flagged as case insensitive with the i switch at the end of the expression. Additionally ([ ]) means the same as ( ), and the use of {1}, which means one and only one time is the default, so is unnecessary.

With all that in mind, the entire expression can be considerably shortened to*:

If that doesn't take care of the problem, let me know - and tell me the criteria for a valid UK postal code. A link to your page would be a great help.

One other thing. regPostcode.test(postcode) doesn't tell us whether or not postcode has been properly defined elsewhere as the value of the postcode input. If it hasn't, then most likely it will always fail the test.

Here's a working demo that includes an example of the shortest and the longest code that would fit the criteria, which are outlined in plain words:

Thanks a lot for the response, I figured out from your post that I wasn't defining where the postcode value was being entered meaning there was no link between the field and the function script. I also changed the expression to what you suggested, all is good and working now, thanks!

Great! However, I just checked the Royal Mail website and the say the definition for a postal code is different:

What is a postcode?

A postcode is a group of numbers and letters that give precise and essential information about a mail item’s destination. It’s made up of several parts, each of which provides us with specific information.

Example: SO31 4NG
This breaks down as follows:
Outward Code: SO31
SO = the postcode area. One or two letters identify the main office the mail initially goes to.
31 = the postcode District. One or two characters, numeric except, occasionally, in central London. This helps to sort the item at the main office.

Inward Code: 4NG
4 = the Sector. A number defining a neighbourhood within a district.
NG = the Unit code. Two letters that identify a group of approximately 15 addresses.

Please make sure you use full, correct postcodes. The Postcode Finder is designed to help you do this.

So theirs is a little more restrictive. With them all codes must begin with two letters followed by one or two letters or numbers, a space, a number, two letters. It's not so bad though, any code that will satisfy the Royal Mail's criteria will also satisfy that setup by the regular expression. However, the regular expression will allow some codes that are not allowed by the Royal Mail's own definition. They should know, though I guess it's possible they've not articulated it properly.