简体   繁体   English

AngularJS-表单上的ng-pattern缺少某些特殊符号(例如美元符号)的情况

[英]AngularJS - ng-pattern on a form failing for certain special symbols such as the dollar sign

I am writing an app with AngularJS 1.5. 我正在用AngularJS 1.5编写应用程序。 I am trying to write a feature where the user can verify their password in the app and I am using ng-messages to try to validate the form. 我正在尝试编写一项功能,以便用户可以在应用程序中验证其密码,并且我正在使用ng-messages来尝试验证表单。

My from has 2 fields: password and confirm password. 我的来源有2个字段:密码和确认密码。 The 2 validation conditions are: both passwords are required and the passwords must match. 这两个验证条件是:两个密码都是必需的,并且密码必须匹配。

The problem I have is the pattern match fails for a few special characters. 我的问题是模式匹配对于某些特殊字符失败。 It fails for the dollar sign but not all special characters. 美元符号失败,但并非所有特殊字符都失败。 I need it to work for all characters. 我需要它来处理所有角色。

Here is my JSFiddle: http://jsfiddle.net/aubz88/gm0obnqf/69/ 这是我的JSFiddle: http : //jsfiddle.net/aubz88/gm0obnqf/69/

A code snippet: 一个代码片段:

  <div>
    <label 
      class="item" 
      ng-class="{ 'has-error' : vm.verifyPassword.password.$invalid && (vm.verifyPassword.$submitted || vm.verifyPassword.$dirty) }">
    <span class="input-label">Password</span>
    <input
      id="password"
      type="password"
      name="password"
      ng-model="vm.data.password"
      placeholder="password"
      required>
    </label>
  </div>

The problem is this line in your JSFiddle, you pass vm.data.password (as a string) to ng-pattern. 问题是您的JSFiddle中的这一行将vm.data.password(作为字符串)传递给ng-pattern。

ng-pattern="vm.data.password"

According to document of Arguments section of ngPattern https://docs.angularjs.org/api/ng/directive/ngPattern#ngPattern-arguments : 根据ngPattern的Arguments部分的文档https://docs.angularjs.org/api/ng/directive/ngPattern#ngPattern-arguments

"AngularJS expression that must evaluate to a RegExp or a String parsable into a RegExp, or a RegExp literal." “必须计算为RegExp或可解析为RegExp或String或RegExp文字的String的AngularJS表达式。”

So AngularJS parses your input into regular expression, that's why some symbols failed, for example $ (dollar sign), as $ is a reserved keyword in regular expression. 因此AngularJS将您的输入解析为正则表达式,这就是为什么某些符号失败的原因,例如$(美元符号),因为$是正则表达式中的保留关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM