【问题标题】:ng-pattern Validation not workingng-pattern 验证不起作用
【发布时间】:2016-06-03 17:40:49
【问题描述】:

我正在尝试使用 angular 和 Bootstrap 进行验证,但由于某些原因,ng-pattern 验证在邮政编码中不起作用。任何帮助都会很好。

这里是FIDDLE LINK

无法粘贴完整代码,但这是我尝试使用 ng-pattern 指令的方式:

<input type="text" class="form-control" id="zipCode" name="zipCode"
       ng-model="auth.zip" ng-pattern="^(\d{5}-\d{4}|\d{5})$" required="">

【问题讨论】:

  • CSS 与您的问题无关。但是您在ng-pattern 中使用的正则表达式可能是;)
  • 我知道 css 不相关。堆栈溢出要求一些代码,所以只是粘贴了一些东西。
  • 堆栈溢出要求您提供小提琴链接中的一些代码。所以,这个问题在未来是有意义的。

标签: angularjs twitter-bootstrap angularjs-directive angular-ui-bootstrap


【解决方案1】:

问题在于,当您将模式内联而不是范围变量放置时,它期望 / 在正则表达式周围,就像一个乱七八糟的东西。

像这样:

/^(\d{5}-\d{4}|\d{5})$/

Fiddle

查看 input docs,查看参数部分,然后查看 ngPattern

【讨论】:

    【解决方案2】:

    你好,你可以这样使用

    $scope.zipNumbr = /^[0-9]{1,6}$/;  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      相关资源
      最近更新 更多