2017-02-25 54 views
0

這是我輸入電子郵件驗證不在angularjs中工作?

[email protected] 

連字符不EMAILID了無效的電子郵件ID 工作,這是我的NG-模式

<div class="form-group"> 
    <div class="input-group"> 
     <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span> 
     <input class="form-control" type="email" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/" noncapitalize required /> 
    </div> 
    <div style="color: red" id="emailError"></div> 
    <span style="color:red;" class="email-error" ng-show="loginForm.submitted && loginForm.email.$error.required">Required</span> 
    <span style="color:red" class="error" ng-show="loginForm.submitted && loginForm.email.$error.pattern">Email not valid</span> 
</div> 
+0

如果你並不真的需要它,您可以刪除'NG-pattern'。 HTML「type =」email「'應該足夠了,不是嗎? – vader

+0

您需要regExp來驗證電子郵件,或者您需要在您的問題中使用特定的「regExp」。 bcoz你似乎有一個問題,你的'正則表達式' – Sravan

+0

@Sravan我需要一個regExp來驗證一個電子郵件,而不是一個特定的謝謝 –

回答

2

這裏是電子郵件驗證一個regexp

^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$

您可以測試這個正則表達式here

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body ng-app=""> 
 

 
<p>Try writing in the input field:</p> 
 

 
<form name="myForm"> 
 
<div class="form-group"> 
 
    <div class="input-group"> 
 
     <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span> 
 
     <input class="form-control" type="text" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/" noncapitalize required /> 
 
    </div> 
 
    <div style="color: red" id="emailError"></div> 
 
    <span style="color:red;" class="email-error" ng-show="myForm.email.$error.required">Required</span> 
 
    <span style="color:red" class="error" ng-show="myForm.email.$error.pattern">Email not valid</span> 
 
</div> 
 
</form> 
 

 

 
</body> 
 
</html>

請執行上面的代碼中

Here is a working DEMO

+0

好姬這是我期望謝謝你 –

+0

很高興幫助你。 :) – Sravan

1

我猜你不想讓「 - 」是在電子郵件地址,這裏是我使用ng-pattern,如果你想「 - 」被允許檢查示例2:

1 )如果 ' - ' 是不允許的:

angular.module('myApp', []) 
 
    .controller('MyController', function($scope) { 
 
    $scope.user = { 
 
     email: '[email protected]' 
 
    }; 
 

 
    $scope.emailPattern = /^(([^-<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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,}))$/; 
 
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="MyController"> 
 

 
    <form name="loginForm"> 
 
    <div class="form-group"> 
 
     <div class="input-group"> 
 
     <input class="form-control" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="emailPattern" noncapitalize required /> 
 
     </div> 
 
     <div style="color: red" id="emailError"> 
 
     <span style="color:red;" class="email-error" ng-show="loginForm.email.$error.required">Required</span> 
 
     <span style="color:red" class="error" ng-show="loginForm.email.$error.pattern">Email not valid, doesn't match the provided pattern</span> 
 
     </div> 
 
    </div> 
 
    </form> 
 

 
</div>

2)如 ' - ' 被允許:

angular.module('myApp', []) 
 
    .controller('MyController', function($scope) { 
 
    $scope.user = { 
 
     email: '[email protected]' 
 
    }; 
 

 
    $scope.emailPattern = /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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,}))$/; 
 
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="MyController"> 
 

 
    <form name="loginForm"> 
 
    <div class="form-group"> 
 
     <div class="input-group"> 
 
     <input class="form-control" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="emailPattern" noncapitalize required /> 
 
     </div> 
 
     <div style="color: red" id="emailError"> 
 
     <span style="color:red;" class="email-error" ng-show="loginForm.email.$error.required">Required</span> 
 
     <span style="color:red" class="error" ng-show="loginForm.email.$error.pattern">Email not valid, doesn't match the provided pattern</span> 
 
     </div> 
 
    </div> 
 
    </form> 
 

 
</div>