简体   繁体   English

AngularJS比较两个输入值无法正常工作

[英]AngularJS comparing two input values doesn't work as expected

I am trying to compare two password values using AngularJS. 我正在尝试使用AngularJS比较两个密码值。 I have the submit button disabled until all the fields are valid. 我禁用了提交按钮,直到所有字段均有效。 However, it is not working like it is supposed to. 但是,它无法正常运行。 As soon as I enter the email and the first password, the submit button becomes enabled. 输入电子邮件和第一个密码后,“提交”按钮即被启用。 But if I type something else in the second password field, then the submit button becomes disabled again. 但是,如果我在第二个密码字段中键入其他内容,则提交按钮将再次被禁用。 How do I do the validation even when nothing is entered in the second password field? 即使在第二个密码字段中未输入任何内容,我该如何进行验证?

Here, is my form. 这是我的表格

<form ng-submit="submit()" name="register" class="form-signin" novalidate>
<h1 class="form-signin-heading text-muted">
    Register
</h1>
<input name="email" ng-model="email" type="email" class="form-control" placeholder="Email address" required>
<p class="help-block" ng-show="register.email.$dirty && register.email.$invalid">Please enter a valid email</p>
<input name="password" ng-model="password" type="password"class="form-control" placeholder="Password" required>
<input name="password_confirm" ng-model="password_confirm" type="password" class="form-control" placeholder="Confirm Password" validate-equals='password'>
<p class="help-block" ng-show="register.password_confirm.$dirty && register.password_confirm.$invalid">The passwords do not match</p>
<button ng-disabled="register.$invalid" class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>

And here is the Javascript file(validateEquals.js) 这是Javascript文件(validateEquals.js)

'use strict';

angular.module('jwtApp').directive('validateEquals', function () {
return {
    require: 'ngModel',
        link: function(scope, element, attrs, ngModelCtrl) {
            function validateEqual(value) {
                var valid = (value === scope.$eval(attrs.validateEquals));
                ngModelCtrl.$setValidity('equal', valid);
                return valid ? value : undefined;
            }       
            ngModelCtrl.$parsers.push(validateEqual);
            ngModelCtrl.$formatters.push(validateEqual);

            scope.$watch(attrs.validateEquals, function() {
                ngModelCtrl.$setViewValue(ngModelCtrl.$viewValue);
            });
        }
};
});

If I can make a suggestion - do this: 如果我可以提出建议,请执行以下操作:

<button ng-disabled="register.$invalid || password != password_confirm" class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>

And not use the validateEqual implementation in your directive. 并且不要在指令中使用validateEqual实现。 Here's a fiddle: http://jsfiddle.net/jochenvanwylick/U3pVM/13972/ with your code and the fix. 这是一个小提琴: http : //jsfiddle.net/jochenvanwylick/U3pVM/13972/及其代码和修复程序。 I wouldn't make it into a directive, nor would I try to hook into the $parsers and $formatters for the element. 我不会将其变成指令,也不会尝试加入元素的$parsers$formatters

try this 尝试这个

.directive('validateEquals', [function () {
    return {
      require: 'ngModel',
      link: function (scope, elem, attrs, ctrl) {
        var firstPassword = '#' + attrs.validateEquals;
        elem.add(firstPassword).on('keyup', function () {
          scope.$apply(function () {
            var v = elem.val()===$(firstPassword).val();
            ctrl.$setValidity('inputmatch', v);
          });
        });
      }
    } 

on html you can check like this 在html上,您可以像这样检查

<input type="password" id="input1" name="input1" ng-model="input1" ng-required/>
<input type="password" id="input2" name="input2" ng-model="input2" ng-required validate-equals="input1"/>

hope will help this solution :) 希望会帮助这个解决方案:)

EDIT 编辑

can show error message like this 可以显示这样的错误消息

<div ng-show="myForm.$error">
  <span  ng-show="myForm.input2.$error.inputmatch">
    Passwords don't match.
  </span>
</div>

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

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