角NG-模式正则表达式code为美国邮编codeS和加拿大邮政codeS [英] Angular ng-pattern regex code for US Zipcodes and Canadian Postal codes

查看:1066
本文介绍了角NG-模式正则表达式code为美国邮编codeS和加拿大邮政codeS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能有人帮我吗?

我要寻找一些正则表达式使用来验证美国邮编codeS和加拿大邮政codeS在AngularJS

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

推荐答案

这应该为你工作:

^(\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.

当使用在Javascript记住,你需要使用 / 分隔符:

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!
}

和直接,作为一个角 NG-模式

ng-pattern="/^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$/"

这篇关于角NG-模式正则表达式code为美国邮编codeS和加拿大邮政codeS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆