简体   繁体   English

angular 1.3中的电子邮件验证

[英]Email validation in angular 1.3

I have a form which has couple of text box and a Submit button. 我有一个表格,其中有几个文本框和一个提交按钮。 One of the text box is an email. 文本框之一是电子邮件。 I am using angular and below is the code I found from one this reference-: http://plnkr.co/edit/T2X02OhKSLBHskdS2uIM?p=preview 我使用的是angular,下面是我从该参考文献中找到的代码: http : //plnkr.co/edit/T2X02OhKSLBHskdS2uIM?p=preview

<form name="inviteUserForm" ng-submit="inviteUser(inviteUserForm.$valid)" novalidate>
  Email: <input type="email" name="input" ng-model="text" required>
  <br>
  <span class="error" ng-show="inviteUserForm.input.$error.required">
    Required!
  </span>
  <span class="error" ng-show="inviteUserForm.input.$error.email">
    Not a valid email!
  </span>
  <br>
  <button type="submit" class="btn btn-default btn-primary btn-shaded ng-scope" ng-disabled="sending" translate="">Send Invite</button>
</form>

Now It as in the demo in the link it allows certain items as a valid email address like-: 现在,就像链接中的演示一样,它允许某些项目作为有效的电子邮件地址,例如:

me@ex = is a Valid emailId me@ex.c = is a Valid emailId me @ ex =是有效的emailId me@ex.c =是有效的emailId

problem is when I hit submit for above emails it never gets send to the email address (because emailId is not valid). 问题是当我点击上述电子邮件的提交时,它永远不会发送到电子邮件地址(因为emailId无效)。

I'd appreciate any feedback. 我会很感激任何反馈。

Update-: 更新-:

Thanks for the comments. 感谢您的评论。 As per your feedback I've updated my code to use ng-pattern now. 根据您的反馈,我已经更新了代码,现在可以使用ng-pattern了。 Below is the screenshot of actual code using ng-pattern. 以下是使用ng-pattern的实际代码的屏幕截图。

在此处输入图片说明

still having the same issue. 仍然有同样的问题。

Use ng-pattern like this. 像这样使用ng-pattern

<input type="email" name="input" ng-model="text" ng-pattern="/^[az]+[a-z0-9._]+@[az]+\\.[az.]{2,5}$/" required > <input type="email" name="input" ng-model="text" ng-pattern="/^[az]+[a-z0-9._]+@[az]+\\.[az.]{2,5}$/" required >

And add one span 并加一个跨度

<span class="error" ng-show="inviteUserForm.input.$error.pattern">
    Invalid email address. !
</span>

in your controller 在您的控制器中

function validate() {
$scope.isValidEmail = false;
                  var pattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                  if ($scope.text) {
                      if (!pattern.test($scope.text)) {
                          $scope.isValidEmail = true;
                      } else {
                          $scope.isValidEmail= false;
                      }
                  }
 }

in your html page 在您的html页面中

<input type="email" name="input" ng-model="text" ng-change="validateEmail()">
 <span ng-show="isValidEmail" class="error">Invalid email address. Please re-enter your email address.</span>
<button type="submit" class="btn btn-default btn-primary btn-shaded ng-scope" ng-disabled="isValidEmail" translate="">Send Invite</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM