角有条件电子邮件验证 [英] Angular conditional email validation

查看:122
本文介绍了角有条件电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何有条件地使用基于另一个作用域变量的presence角的电子邮件验证?我通过文档看了,但我能找到触发电子邮件验证的唯一方法是使用 INPUT TYPE =电子邮件,在这种情况下,我不能因依赖使用另一个指令键入=文字

How can I conditionally use angular's email validation based on the presence of another scope variable? I looked through the documentation but the only way I can find to trigger email validation is to use input type="email", which in this case I cannot use due to the dependence of another directive on type="text".

理想我想无论是喜欢基于另一个范围变量的值赋给NG-匹配=电子邮件,或只是验证电子邮件编程上提交。从理论上讲我可能只是一个单独的电子邮件验证正则表达式,但如果可能的话,我想,因为我使用的其他任何地方使用角度的验证。

Ideally I'd either like to assign ng-match="email" based on the value of another scope variable, or just validate the email programatically on submission. In theory I could just a separate email validation regex but if possible I'd like to use Angular's validation since I use that everywhere else.

感谢

-
编辑:为了澄清,我特别想用棱角分明的原生电子邮件验证在任何解决方案,我最终使用

-- To clarify, I specifically would like to use angular's native email validation in whatever solution I end up using.

推荐答案

您应该使用 NG-要求 NG-模式上的电子邮件输入字段。

You should use ng-required and ng-pattern on the email input field.

<input type="text" ng-model="email" ng-required="emailRequired" ng-pattern="emailPattern" />

和再定义 emailPattern 作为对 $范围功能您的控制器。

And then define emailPattern as function on the $scope of your controller.

$scope.emailPattern = (function() {
var regexp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return {
    test: function(value) {
        if( $scope.emailRequired === false ) return true;
        else return regexp.test(value);
    }
};

})();

下面拨弄实现你想要什么:

The following fiddle implements what you want:

http://jsfiddle.net/9SSE4/

这篇关于角有条件电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆