Search code examples
regexangularjsemailangularjs-directiveng-pattern

How to validate email id in angularJs using ng-pattern


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

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

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>

Solution

  • 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 = '[email protected]';
    }
    </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>
    

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

    Live example: http://plnkr.co/edit/T2X02OhKSLBHskdS2uIM?p=info

    UPD:

    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:

    The validator sets the pattern error key if the ngModel.$viewValue does not match a RegExp

    <script>
    function Ctrl($scope) {
      $scope.text = '[email protected]';
      $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