简体   繁体   English

ng-message无法使用重新输入密码验证AngularJS

[英]ng-message not working with retype password validation AngularJS

Following is my code, which is working fine for password, but somehow Retype password validation is not working. 以下是我的代码,该代码可以很好地使用密码,但是以某种方式重新输入密码验证无效。 Let me know what I am doing wrong here. 让我知道我在这里做错了。

  <div class="form-group has-feedback">
    <input type="password" class="form-control" placeholder="Password" ng-model="vm.user.password" name="password" required="" ng-minlength="6" ng-maxlength="20" />
    <div class="help-block" ng-messages="userForm.password.$error" ng-if="userForm.password.$touched">
      <p ng-message="minlength, maxlength, required">Please enter a password containing 6 to 20 characters.</p>
    </div>        
  </div>
  <div class="form-group has-feedback">
    <input type="password" class="form-control" placeholder="Retype password" name="retypePassword" required="" ng-model="vm.user.retypePassword">
    <div class="help-block" ng-messages="userForm.retypePassword.$error" ng-if="userForm.retypePassword.$touched">
      <p ng-message when="vm.user.retypePassword !== vm.user.password" >Please retype your correct password.</p>
    </div>

In DOCS they mentioned that we can use expression, but somehow its not validating. DOCS中,他们提到我们可以使用表达式,但是某种程度上它不能验证。

I believe you've got the usage wrong. 我相信您的用法有误。 the when attribute is used in a multi-message situation and it checks to see if the defined error exists, using a string key or array of keys, just like the ng-message attribute, basically. 在多消息情况下使用when属性,它基本上使用ng-message属性,使用字符串键或键数组检查是否存在定义的错误。

You can create a custom validator with a directive, such as explained here . 您可以创建自定义验证了指令,如解释在这里

You can also simply manipulate the $error object with an ng-change function something like this: 您还可以使用ng-change函数简单地操作$ error对象,如下所示:

<input type="password" ng-model="vm.user.retypePassword"
    ng-change="setMatchError(userForm.retypePassword,userForm.password)">

Then in your controller 然后在您的控制器中

vm.setMatchError = function(input1, input2) {
    if (input1.$viewValue!=input2.$viewValue) {
         input1.$error.match = true;
    } else {
         delete input1.$error['match'];
    }
 }

finally, you'll be able to refer to that using the ng-message directive 最后,您将可以使用ng-message指令来引用它

<p ng-message="match">Your passwords must match!</p>

or 要么

<p ng-message when="match">Your passwords must match!</p>

Though this last method would probably be frowned upon form manipulating the angular objects directly in application code. 尽管最后一种方法可能不适合直接在应用程序代码中直接操纵角度对象的形式。

One thing to note , however, is that it seems tha if an input has not passed its html5 validation, the model doesn't get updated, so you won't be able to refer to the model values. 但是要注意的一件事是,如果输入未通过其html5验证,则该模型不会得到更新,因此您将无法引用该模型值。


One last option: you can ignore the ng-message(s) directives entirely on the confirmation password (since it doesn't really give you much) and just use ng-if and ng-change and direct model validation. 最后一个选择:您可以完全忽略确认密码上的ng-message(s)指令(因为它实际上并不能给您太多),而只需使用ng-if和ng-change以及直接模型验证即可。

In this case you can use a directive use-form-error that helps to create your own checks, like the ng-required, ng-minlength or ng-pattern. 在这种情况下,您可以使用指令use-form-error来帮助创建自己的检查,例如ng-required,ng-minlength或ng-pattern。

<form name="ExampleForm">
  <label>Password</label>
  <input ng-model="password" required />
  <br>
  <label>Confirm password</label>
  <input ng-model="confirmPassword" required use-form-error="dontMatch" use-error-expression="password && confirmPassword && password!=confirmPassword" />
  <div ng-messages="ExampleForm.$error" class="errors">
    <div ng-message="dontMatch">
      Passwords Do Not Match!
    </div>
  </div>
</form>

Live example jsfiddle . 实时示例jsfiddle

As described in the documentation here you would need to give your error name in the when attribute if you need to use expression you need put your expression in messages block,so change your div block like this 如此处的文档所述,如果您需要使用表达式,则需要在when属性中输入错误名称,您需要将表达式放在messages块中,因此请像这样更改div块

<div class="help-block" ng-messages="multipleValueExpression">
      <p ng-message when="value1" >Please retype your correct password.</p>
</div>

or you can achieve the same by changing when with when-exp like this 或者您可以通过使用when-exp更改when来达到相同目的

<p ng-message when-exp="vm.user.retypePassword !== vm.user.password" >Please retype your correct password.</p>

good luck 祝好运

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

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