Reputation:
I've got the following to add spaces to a postcode:
function isValidPostcode(p) {
var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
return postcodeRegEx.test(p); }
function formatPostcode(p) {
if (isValidPostcode(p)) {
var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
return p.replace(postcodeRegEx,"$1 $2");
} else {
return p;
}}
It checks the postcode and adds a space. It works for postcodes with SA143TG and adds a space to make SA14 3TQ, but with SA13TQ it doesn't work.
Any ideas?
Upvotes: 0
Views: 799
Reputation: 232
Instead of this line
var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
try this
var postcodeRegEx = /([A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2})/i;
Upvotes: 1