javascript - 电子邮件黑名单和验证

标签 javascript angularjs validation blacklist

我是 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>

Here is a Fiddle-Demo

最佳答案

我为您的问题创建了 JSFiddle。

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/

相关文章:

angularjs - Protractor browser.driver.getCurrentUrl 与 browser.getCurrentUrl

java - 如何验证 Struts (Java) 中的 ActionForm 是否已更改?

javascript - 使用 Chrome 控制台跟踪 javascript 的事件

javascript - Safari 刷新阻止 jQuery 执行

javascript - 如何为 Angular 表进行分页?

ruby-on-rails - Rails STI 验证继承

Scalaz:ValidationNel 的映射到 Map 的 ValidationNel

javascript - 使用ajax作为评论表单,它给了我500内部错误,但没有脚本它可以工作,这意味着我的脚本是错误的;对吗?

javascript - 你如何在angularjs中重用模板

javascript - 如何通过服务强制更新 AngularJS 中的 View ?