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
This should work for you:
^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$
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)$/"
来源:https://stackoverflow.com/questions/21197798/angular-ng-pattern-regex-code-for-us-zipcodes-and-canadian-postal-codes