2017-02-25 8 views
0

が、これは、これはここに私のNGパターンangularjsで電子メールの検証が機能していませんか?

<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'が必要です。あなたはあなたの '正規表現 'に問題があるようです。 – Sravan

+0

@スラヴン私は特定のものではなく、電子メールを検証するためにregExpが必要です。 –

答えて

2

である電子メールの検証のためのregexpある

[email protected] 

ハイフンがEMAILIDなっ無効な電子メールID に動作していない私の入力です。

^[\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>

この正規表現をテストすることができます上のコード

012を実行してください「 - 」私はあなたが許可したくないと思い

+0

nice Jiこれは私が期待しているものです –

+0

お手伝いをしてうれしいです。 :) – Sravan

1

メールアドレスであることをが、ここで私はng-patternを使用して得たものであり、あなたがしたい場合は「 - 」のチェック第二の例許可する:

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>

関連する問題