覆盖AngularJS默认电子邮件验证程序

时间:2015-01-14 20:47:46

标签: javascript angularjs html5 angularjs-directive angularjs-validation

我想覆盖AngularJS的电子邮件验证程序。我希望它使用我的自定义字符串来验证电子邮件地址。 我在他们的文档中找到的代码是这样的:     JS:

   var app = angular.module('registrationApp', []);

    app.directive('overwriteEmail', function() {
      var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i;

      return {
        require: 'ngModel',
        restrict: '',
        link: function(scope, elm, attrs, ctrl) {
            if (ctrl && ctrl.$validators.email) {

                // this will overwrite the default Angular email validator
                ctrl.$validators.email = function(modelValue) {
                    return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
                }
            }
        }
    }
});

HTML:

<div data-ng-app="registrationApp" data-ng-init="">
  <form name="form" class="css-form" novalidate>
  <div>
      Overwritten Email:
      <input type="email" ng-model="myEmail" overwrite-email name="overwrittenEmail" />
      <span ng-show="form.overwrittenEmail.$error.email">This email format is invalid!     </span><br>
      Model: {{myEmail}}
    </div>
  </form>
</div>

但代码不起作用。引发的错误是:

  

TypeError:无法读取属性&#39;电子邮件&#39;未定义的       在链接(http://localhost:63342/goga_tests/js/registration.js:39:32)       在N(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:54:372)       at g(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:256)       at g(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:273)       在N(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:54:313)       at g(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:256)       在N(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:54:313)       at g(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:256)       在http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:46:377       在http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:18:314   

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:11)

您正在使用的

角度版本1.2.26不会在ngModel控制器实例上添加$validators特殊属性。你需要1.3.x版本的角度。

如果您有1.3.x,您可以按原样使用您的指令,并且您可以将优先级2添加为大于ng-model(为1),以便您的指令在{{{{}之前设置验证程序1}}指令轮流:

ng-model

<强>演示

&#13;
&#13;
app.directive('overwriteEmail', function() {
  var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i;

  return {
    require: 'ngModel',
    restrict: '',
    priority: 2,
    link: function(scope, elm, attrs, ctrl) {
      if (ctrl && ctrl.$validators.email) {
        // this will overwrite the default Angular email validator
        ctrl.$validators.email = function(modelValue) {
          return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
        }
      }
    }
  }
});
&#13;
var app = angular.module('app', []);

app.directive('overwriteEmail', function() {
  var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i;

  return {
    require: 'ngModel',
    restrict: '',
    priority: 1,
    link: function(scope, elm, attrs, ctrl) {
      if (ctrl && ctrl.$validators.email) {
        // this will overwrite the default Angular email validator
        ctrl.$validators.email = function(modelValue) {
          return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
        }
      }
    }
  }
});
&#13;
&#13;
&#13;

您还可以坚持使用<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script> <div ng-app="app"> <div> <form name="form" class="css-form" novalidate> <div> Overwritten Email: <input type="email" ng-model="myEmail" overwrite-email name="overwrittenEmail" /> <span ng-show="form.overwrittenEmail.$error.email">This email format is invalid! </span> <br>Model: {{myEmail}} </div> </form> </div> </div>并使用ng-pattern来显示无效模式的消息。

form.overwrittenEmail.$error.pattern

<强>演示

&#13;
&#13;
<input type="email" ng-pattern="/^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i" 
                    ng-model="myEmail" name="overwrittenEmail" />
   <span ng-show="form.overwrittenEmail.$error.pattern">
&#13;
var app = angular.module('app', []);
&#13;
&#13;
&#13;