s***@hotmail.com
2010-10-13 07:07:42 UTC
A postcode in the U.K. normally has 1 or 2 characters, followed by 1
or 2 numbers, a space, a number and two characters. Validating a form
value to see it matches this criteria is easy (using regular
expressions) and documented all over the web.
However, I have not been able to trace a method of inserting a space
in the correct place if the user has not done so. For example, if the
user types "me12tr", changing it to upper case is easy, so we have
"ME12TR", but I would like in this instance to automatically insert a
space between the 1 and the 2, without having to prompt them to do so.
The start of U.K. postcodes vary (M1, CT2, SW1A are all valid), but
there seems always to be one digit and two characters after the space,
at the end of the postcode. So is there a way in Javascript I can
insert a space between the third and fourth characters *counting from
the right* of the entered string, to correct a postcode entered
without the required space?
Thank you for any advice you can give.
Steve
or 2 numbers, a space, a number and two characters. Validating a form
value to see it matches this criteria is easy (using regular
expressions) and documented all over the web.
However, I have not been able to trace a method of inserting a space
in the correct place if the user has not done so. For example, if the
user types "me12tr", changing it to upper case is easy, so we have
"ME12TR", but I would like in this instance to automatically insert a
space between the 1 and the 2, without having to prompt them to do so.
The start of U.K. postcodes vary (M1, CT2, SW1A are all valid), but
there seems always to be one digit and two characters after the space,
at the end of the postcode. So is there a way in Javascript I can
insert a space between the third and fourth characters *counting from
the right* of the entered string, to correct a postcode entered
without the required space?
Thank you for any advice you can give.
Steve