【问题标题】:Password match angularjs validation密码匹配 angularjs 验证
【发布时间】:2015-10-18 05:54:20
【问题描述】:

尝试使用 Angular js 中的自定义指令匹配密码。虽然我看过几个谷歌教程,但我无法让它工作。我创建了一个 Plunker,显示它在 plunker 上不起作用。

HTML:

    <div class="form-group" ng-class="{ 'has-error': form.password.$invalid && !form.username.$pristine }">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" class="form-control" ng-model="user.password" required ng-minlength="6" ng-maxlength="12"/>
    </div>
    <div class="form-group" ng-class="{ 'has-error': form.confirm-password.$invalid && !form.confirm-password.$pristine }">
        <label for="confirm-password">Confirm Password</label>
        <input type="password" name="confirm-password" id="confirm-password" class="form-control" ng-model="user.confirmpwd" required equal="{{user.password}}"/>
        <span ng-show="user.confirmpwd.$error.equal" class="help-block">Password does not match above</span>
    </div>

JAVASCRIPT:

app.directive('equal', [
function() {

var link = function($scope, $element, $attrs, ctrl) {

  var validate = function(viewValue) {
    var comparisonModel = $attrs.equal;
      console.log(viewValue + ':' + comparisonModel);

    if(!viewValue || !comparisonModel){
      // It's valid because we have nothing to compare against
      ctrl.$setValidity('equal', true);
    }

    // It's valid if model is lower than the model we're comparing against
    ctrl.$setValidity('equal', viewValue === comparisonModel );
    return viewValue;
  };

  ctrl.$parsers.unshift(validate);
  ctrl.$formatters.push(validate);

  $attrs.$observe('equal', function(comparisonModel){
        return validate(ctrl.$viewValue);
  });

};

return {
  require: 'ngModel',
  link: link
};
}]);

问题似乎与自定义指令有关,它似乎没有正确绑定到 ngModel 项?我觉得我一定错过了一些简单的东西,我刚刚开始学习 AngularJS。

【问题讨论】:

    标签: angularjs angularjs-directive angular-directive


    【解决方案1】:

    密码字段绑定应该可以工作,但您正在验证密码字段至少应为 6 个字符长,这意味着只有在您输入 6 个或更多字符后它才会绑定到模型。在那之前它将是undefined,这就是你在我假设的console.log 语句中得到的。

    但是还有其他问题。将不会显示错误消息,因为您的字段名称是confirm-password。您应该将其命名为 confirmPassword 或不带破折号的名称。该名称被 Angular 用作对象属性,JavaScript 对象键不能包含破折号。

    因此,表单的密码确认部分应如下所示:

    <div class="form-group" ng-class="{ 'has-error': form.confirmPassword.$invalid && !form.confirmPassword.$pristine }">
        <label for="confirm-password">Confirm Password</label>
        <input type="password" name="confirmPassword" id="confirm-password" class="form-control" ng-model="user.confirmpwd" required equal="{{user.password}}"/>
        <span ng-show="form.confirmPassword.$error.equal" class="help-block">Password does not match above</span>
    </div>
    

    【讨论】:

    • 啊,那些该死的破折号。我改变了它,一切都很完美,除了你提到的,密码是未定义的,直到它至少有 6 个字符。在这里做的最好的事情是什么?如果 compareModel(第一个密码)是
    • @jekelija 我可能会显示密码字段的验证消息,通知用户密码太短或太长。这样一来,为什么不执行密码确认验证就很明显了——因为要确认的密码一开始就无效。
    • @BohuslavBurghardt 另外,您不应使用解析器和格式化程序来验证字段。验证器就是因为这个原因而存在的。这是一个使用叉子的叉子:plnkr.co/edit/ogJIlMyqWrOlXpyv9psB?p=preview
    • @JBNizet 太棒了,这也适用于我。感谢那。只是学习角度,似乎有 10 种方法可以给每只猫剥皮。
    猜你喜欢
    • 2014-10-18
    • 2017-03-16
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2016-08-27
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多