ng-pattern不适用于特定的电子邮件地址?

时间:2017-05-09 19:30:17

标签: javascript angularjs regex

我想使用ng-pattern验证特定的域电子邮件验证,假设我想要这种模式ab123z@domain.comab1234@domain.com这些是我必须在用户输入值时应用的两个条件。我在emailFromat下方尝试过,但在所有情况下都显示无效?任何更好的方法?

main.html

<div layout="row">
    <md-input-container flex="100">
        <label>Cc</label>
        <input type="email" name="email" ng-model="notifyCtrl.cc" ng-list="," ng-pattern="pattern(user.type)">
        <div class="help-block" ng-messages="notifyForm.email.$error" ng-show="notifyForm.email.$touched && notifyForm.email.$invalid">
            <div ng-if="user.type === 'notify'">
                <div ng-message="pattern">
                    An email name must only contain a-z, A-Z, 0-9, or _ characters.(ie. ab123c@tad.com, ab1234@tad.com
                </div>
            </div>
        </div>
    </md-input-container>
</div>

ctrl.js

var emailFormat = "/^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@+(?: |[a-z0-9-]+\.com|com|[a-z0-9-])$/" 

$scope.pattern = function(type){ 
    if(type === 'notify') {
        return emailFormat; 
    }  
};

1 个答案:

答案 0 :(得分:0)

从正则表达式中删除引号。没有必要。 https://regex101.com/r/njkXrr/1

var emailFormat = /^[a-zA-Z]{2}[a-z0-9!#$%&'*+/=?^_`{|}~.-]*[a-zA-Z]{2}@+(?: |[a-z0-9-]+\.com|com|[a-z0-9-])$/

$scope.pattern = function(type){ 
    if(type === 'notify') {
        return emailFormat; 
    }  
};
相关问题