覆盖 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>
但是代码不起作用。引发的错误是:
类型错误:无法读取未定义的属性"电子邮件" 在链接 (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) 在 G (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:256) 在 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) 在 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) 在 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.2.26
不会在 ngModel 控制器实例上添加$validators
特殊属性。您需要 1.3.x 版本的 angular。
如果你有 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>
- javascript中添加验证电子邮件的问题
- 如何测试变量是否只有字母而不是abc1.关于验证电子邮件
- jquery通过ajax验证电子邮件,然后再通过ajax提交表单
- JavaScript通过SMTP验证电子邮件
- 如何使用Selenium Web驱动程序验证电子邮件
- 使用不带正则表达式的jscript验证电子邮件地址
- 在passport.js中使用令牌验证电子邮件
- 使用JavaScript或JQuery验证电子邮件地址
- 使用Regex验证电子邮件id长度
- 如何在文本字段Appcelerator中输入文本后验证电子邮件
- Javascript/jQuery像浏览器一样验证电子邮件
- 如何使用正则表达式根据javascript中的php电子邮件注入验证电子邮件地址
- 如何在javascript中验证电子邮件
- 在使用jquery验证电子邮件之后添加一个类
- 验证电子邮件时无法阻止表单提交
- 使用正则表达式验证电子邮件地址
- 调用外部JS函数验证电子邮件
- JavaScript 验证电子邮件地址没有响应
- 在 JavaScript 中验证电子邮件地址
- 验证电子邮件地址