0

I am having a certain issue in defining the regex for UK postal code. I have made the regex which is in accordance with the client requirement, which is working fine in the regexr.com test. but when I tried to test the same in the javascript i had the situation in which it is accepting one more letter in the last.

RULE : part before space can have 2/3/4 characters but 1st should be numeric. and part in the right can only have 3 characters in which 1st should be number and other two must be letters.

Regex: :

 /^[A-Za-z](([A-Za-z]|[0-9]){1,2})?([A-Za-z]|[0-9]) [0-9][A-Za-z]{2}/g 

and my javascript Code (through which I am checking it):

 var str = /^[A-Za-z](([A-Za-z]|[0-9]){1,2})?([A-Za-z]|[0-9]) [0-9][A-Za-z]{2}/g ;
 var res = str.test($scope.pincode);

ISSUE : the valid formats are :

A12 1ES A122 1ES ASD 1RD

but it is also accepting

A12 1ESD

can someone help me in the same?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
vaibhav
  • 720
  • 1
  • 10
  • 31

0 Answers0