且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角有条件电子邮件验证

更新时间:2022-06-12 21:16:13

您应该使用 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/