电子邮件检查角度

时间:2014-10-02 10:52:01

标签: angularjs

我正在使用angularjs来验证jsp页面中的电子邮件输入。

<p>Email:</p>
<input type="email" name="email1" ng-model="email1" required/>
<div class="custom-error" ng-show="loginForm.email1.$dirty && loginForm.email1.$invalid">Invalid:
<span ng-show="loginForm.email1.$error.required">Email is required.</span>
<span ng-show="loginForm.email1.$error.email">Please, write a valid email address.</span>
</div>

这个代码功能,但是当我把邮件放在没有.it的情况下,.com就像ciccio.mario@gmail一样没有发现错误。我该如何修复这个错误?

1 个答案:

答案 0 :(得分:1)

您可以使用ng-pattern进行自定义验证

&#13;
&#13;
var app = angular.module("app", []);


function MainCtrl() {}





angular.module("app").controller("MainCtrl", MainCtrl);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="MainCtrl">

    <form name="loginForm">
      <input type="email" name="email1" ng-model="email1" required ng-pattern="/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/" required />
      <div class="custom-error" ng-show="loginForm.email1.$dirty && loginForm.email1.$invalid">Invalid:
        <span ng-show="loginForm.email1.$error.pattern">Not a valid email!</span>
        <span ng-show="loginForm.email1.$error.required">This field is required!</span>
      </div>
    </form>
  </div>

</div>
&#13;
&#13;
&#13;