Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-pattern regex code for US Zipcodes and Canadian Postal codes [closed]

I could anybody help me out?

I am looking for some regexp to use to validate US Zip codes and Canadian Postal codes in AngularJS

like image 646
Sonia Brami Avatar asked Jan 12 '23 10:01

Sonia Brami


1 Answers

This should work for you:

^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$

http://regex101.com/r/nO4zF5

Remove the ^ and $ anchors if you don't want to match the start and end of the string.

When using in Javascript remember that you need to use the / delimiter:

if (/^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$/.test(str)) {
    // it's a match!
}

And directly, as an Angular ng-pattern:

ng-pattern="/^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$/"
like image 196
brandonscript Avatar answered Jan 15 '23 11:01

brandonscript