如何通过一个输入控件应用多个ng模式 [英] How to apply Multiple ng-pattern with one input control

查看:48
本文介绍了如何通过一个输入控件应用多个ng模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Angularjs验证文本框中的邮政编码和电话号码.但这不起作用

I am trying to Validate Postal code and Phone Number in Text box using Angularjs. But it's not working

 <input type="text" class="form-control errorfields" id="postalCode"
 name="postalCode" ng-model="postalCode" 
 ng-pattern="/(^(\d{5}(-\d{4})?|[A-CEGHJ-NPRSTVXY]\d[A-CEGHJ-NPRSTV-Z]
  ?\d[A-CEGHJ-NPRSTV-Z]\d)$)||(^[0-9])/"  required>

推荐答案

首先,如mico所指出的那样,您的正则表达式语法中有双管道是无效的,并且会破坏您的表达式.

First, having double pipes in your regex syntax as mico pointed out, is invalid and will break your expression.

第二,ng模式每个输入只能验证一个模式.如果您需要它来验证或验证,则需要走两条路线之一,创建自定义指令,或向控制器添加一些逻辑,以确定我们应该检查哪个表达式,然后使用数据绑定将其传递给ng-pattern.在有角度的世界中,这是不好的做法,因此,最好的选择是做出指示.

Second, ng-pattern can only validate one pattern per input. If you need it to validate either or, you will need to go one of two routes, create a custom directive, or add some logic to the controller to determine which expression we should check against and pass it into ng-pattern using data binding. This is bad practice in the angular world, so your best bet is to make a directive.

这篇关于如何通过一个输入控件应用多个ng模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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