我是 Angular 新手,我尝试进行简单的黑名单检查。目前我有两个文本可以用 ng-show 显示和隐藏。第一个应该在邮件模式错误时显示,而在正确和/或在黑名单上时隐藏。
我的问题是我不知道如何实现该模型。目前它是通过复选框模拟的。也许有人有提示。
<div class="controls">
<input type="checkbox" ng-model="checked" ng-init="checked=true" /><br/>
<input type="email" id="inputEmail" placeholder="Email" ng-model="email" required>
<div class="hint">
<h4 name="mailValidator" ng-if="checked" ng-show="true">Invalid Email</h4>
<h4 name="checkBlacklist" ng-if="!checked" ng-show="true">Email is not allowed</h4>
</div>
最佳答案
我为您的问题创建了 JSFiddle。
查看:
<div ng-controller="MyCtrl">
<input placeholder="Email" ng-model="email" ng-pattern="emailRegExp" ng-class="{ error: !email }" />
<h4 name="mailValidator" ng-show="!email">Invalid Email</h4>
<h4 name="checkBlacklist" ng-show="email && inBlackList">Email is not allowed</h4>
</div>
Controller :
function MyCtrl($scope) {
$scope.inBlackList = false;
//get from DB in your case
var bannedEmail = ['qwe@qwe.qwe','qwe1@qwe.qwe','qwe2@qwe.qwe']
$scope.emailRegExp = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
$scope.$watch('email', function(newValue) {
if(newValue){
if(bannedEmail.indexOf(newValue) > -1) {
$scope.inBlackList = true;
}
else {
$scope.inBlackList = false;
}
}
});
}
关于javascript - 电子邮件黑名单和验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22222784/