Max
Max

Reputation: 27

RegEx Jquery Postcode for europe or uk

Trying to allow either UK postcode (GF433ED) or European postcode (12345) return true or false but the below code always returns false:

 function valid_postcode(postcode) {
     postcode = postcode.replace(/\s/g, "");
     alert(postcode);
     var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}|^[0-9]{5}$/;
     return regex.test(postcode); 
 }

Any help would be greatly appreciated.

Thanks

Upvotes: 1

Views: 219

Answers (2)

Max
Max

Reputation: 27

Thanks everyone it was all a bit of both (forgot to post answer)

See working regex:

/^[A-z]{1,2}[0-9]{1,2}\s*[0-9][A-z]{2}$|^[0-9]{5}$/

Upvotes: 0

Chandra Kumar
Chandra Kumar

Reputation: 4205

Use \s* for allowing an optional space.

/[A-z]{1,2}[0-9]{1,2}\s*[0-9][A-Z]{2}/i

Upvotes: 1

Related Questions