本文介绍了如何使用 ng-pattern 在 angularJs 中验证电子邮件 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ng-pattern 指令验证 angularJs 中的电子邮件 ID 字段.

Am trying to validate an Email id field in angularJs using ng-pattern directive.

但是我是 AngularJs 的新手.我需要在用户输入错误的电子邮件 ID 后立即显示错误消息.

But am new to AngularJs. I need to show an error message as soon as the user enters the wrong email id.

我下面的代码正在尝试解决.帮助我使用 ng-pattern 获得正确的结果.

The code which I have below is am trying to solve. Help me out with using ng-pattern for getting the proper result.

<script type="text/javascript" src="/Login/script/ang.js"></script>
<script type="text/javascript">
    function Ctrl($scope) {
        $scope.text = 'enter email';
        $scope.word = /^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/;
    }
</script>
    </head>
<body>
    <form name="myform" ng-controller="Ctrl">
        <input type="text" ng-pattern="word" name="email">
        <span class="error" ng-show="myform.email.$error.pattern">
            invalid email!
        </span>
        <input type="submit" value="submit">
    </form>
</body>

推荐答案

如果您想验证电子邮件,请使用带有 type="email" 而不是 type="text" 的输​​入.AngularJS 具有开箱即用的电子邮件验证功能,因此无需为此使用 ng-pattern.

If you want to validate email then use input with type="email" instead of type="text". AngularJS has email validation out of the box, so no need to use ng-pattern for this.

以下是原始文档中的示例:

Here is the example from original documentation:

<script>
function Ctrl($scope) {
  $scope.text = 'me@example.com';
}
</script>
<form name="myForm" ng-controller="Ctrl">
  Email: <input type="email" name="input" ng-model="text" required>
  <br/>
  <span class="error" ng-show="myForm.input.$error.required">
    Required!</span>
  <span class="error" ng-show="myForm.input.$error.email">
    Not valid email!</span>
  <br>
  <tt>text = {{text}}</tt><br/>
  <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
  <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
  <tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
  <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
  <tt>myForm.$error.email = {{!!myForm.$error.email}}</tt><br/>
</form>

有关更多详细信息,请阅读此文档:https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

For more details read this doc: https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

实例:http://plnkr.co/edit/T2X02OhKSLBHskdS2uIM?p=info

更新:

如果您对内置的电子邮件验证器不满意,并且想要使用自定义 RegExp 模式验证,则可以应用 ng-pattern 指令并根据 文档 错误信息可以这样显示:

If you are not satisfied with built-in email validator and you want to use your custom RegExp pattern validation then ng-pattern directive can be applied and according to the documentation the error message can be displayed like this:

验证器设置模式错误键,如果 ngModel.$viewValue与 RegExp 不匹配

<script>
function Ctrl($scope) {
  $scope.text = 'me@example.com';
  $scope.emailFormat = /^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/;
}
</script>
<form name="myForm" ng-controller="Ctrl">
  Email: <input type="email" name="input" ng-model="text" ng-pattern="emailFormat" required>
  <br/><br/>
  <span class="error" ng-show="myForm.input.$error.required">
    Required!
  </span><br/>
  <span class="error" ng-show="myForm.input.$error.pattern">
    Not valid email!
  </span>
  <br><br>
  <tt>text = {{text}}</tt><br/>
  <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
  <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
  <tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
  <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
  <tt>myForm.$error.pattern = {{!!myForm.$error.pattern}}</tt><br/>
</form>

Plunker:https://plnkr.co/edit/e4imaxX6rTF6jfWbp7mQ?p=preview

这篇关于如何使用 ng-pattern 在 angularJs 中验证电子邮件 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 02:15