电子邮件黑名单和验证

时间:2014-03-06 11:14:12

标签: 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

2 个答案:

答案 0 :(得分:0)

如果您使用AngularJS forms验证,则可以执行第一个文本

<form name="form" class="css-form" novalidate>
    <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>
    <div class="hint">
        <h4 name="mailValidator" ng-show="form.uEmail.$error.email && !onBlacklist">Invalid Email</h4>
        <h4 name="checkBlacklist" ng-show="onBlacklist">Email is not allowed</h4>
    </div>
</form>

然后在控制器中你可以观察电子邮件的变化,当它被列入黑名单时将onBlacklist设置为true,希望有所帮助

答案 1 :(得分:0)

我为你的问题创建了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>

控制器:

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;
           }
       }
   });      
}