我想使用 ng-pattern
验证特定的域电子邮件验证,假设我希望这种模式ab123z@domain.com
或ab1234@domain.com
这是用户有输入值时必须应用的两个条件。我在下面尝试过emailFromat
但它在所有情况下都显示无效?有什么更好的方法吗?
主.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>
控制.js
var emailFormat = "/^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@+(?: |[a-z0-9-]+.com|com|[a-z0-9-])$/"
$scope.pattern = function(type){
if(type === 'notify') {
return emailFormat;
}
};
从正则表达式中删除引号。没有必要。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;
}
};