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

Override AngularJS Default Email Validator

我想覆盖 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: Cannot read property 'email' of undefined at link (http://localhost:63342/goga_tests/js/registration.js:39:32) at 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) at 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) at 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) at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:46:377 at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:18:314

在此先感谢您的帮助!

您正在使用的

angular 版本 1.2.26 不会在 ngModel 控制器实例上添加 $validators 特殊 属性。您需要 angular.

的 1.3.x 版本

如果你有 1.3.x 你可以按原样使用你的指令,你可以添加优先级 2 使其大于 ng-model(即 1),这样你的指令在 ng-model 指令轮到它之前设置验证器:

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);
        }
      }
    }
  }
});

演示

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);
        }
      }
    }
  }
});
<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 显示消息。

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

演示

var app = angular.module('app', []);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/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" ng-pattern="/^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i" name="overwrittenEmail" />
        <span ng-show="form.overwrittenEmail.$error.pattern">This email format is invalid!     </span>
        <br>Model: {{myEmail}}
      </div>
    </form>
  </div>
</div>