【问题标题】:How to check Password and Confirm Password Same in AngularJS [duplicate]如何在AngularJS中检查密码和确认密码相同[重复]
【发布时间】:2015-09-17 09:50:04
【问题描述】:

您好,谁能帮助我在 AngularJS 中验证密码和确认密码字段

【问题讨论】:

  • 请仔细阅读 Stackoverflow 在how to ask questions 上的指南以了解未来的问题。
  • 请在此处发布任何问题之前进行一些初步研究。无论如何,下面的答案会对您有所帮助。

标签: javascript angularjs


【解决方案1】:

有很多方法可以实现这一目标。一种方法是使用指令

<input type="password" name="confirmPassword" 
    ng-model="registration.user.confirmPassword"
    required 
    compare-to="registration.user.password" />

<div ng-messages="registrationForm.confirmPassword.$error"
  ng-messages-include="messages.html"></div>

//指令

var compareTo = function() {
  return {
    require: "ngModel",
    scope: {
        otherModelValue: "=compareTo"
    },
    link: function(scope, element, attributes, ngModel) {

        ngModel.$validators.compareTo = function(modelValue) {
            return modelValue == scope.otherModelValue;
        };

        scope.$watch("otherModelValue", function() {
            ngModel.$validate();
        });
    }
  };
};

module.directive("compareTo", compareTo);

Reference

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2018-05-07
    • 2019-04-12
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多