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

北战南征 提交于 2019-12-22 08:51:51

问题


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


回答1:


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)$/"


来源:https://stackoverflow.com/questions/21197798/angular-ng-pattern-regex-code-for-us-zipcodes-and-canadian-postal-codes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!