Skip to content
Related Articles

Related Articles

Improve Article

AngularJS | ng-pattern Directive

  • Last Updated : 29 Mar, 2019

The ng-pattern Directive in AngularJS is used to add up pattern (regex pattern) validator to ngModel on input HTML element. It is used to set the pattern validation error key if input field data does not match a RegExp that is found by evaluating the Angular expression specified in the ngpattern attribute value.

Syntax:

<element ng-pattern="expression"> Contents... </element>

Example 1: This example uses ng-pattern Directive to check the password pattern.




<!DOCTYPE html>
<html>
  
<head>
    <title>ng-pattern Directive</title>
      
    <script src=
    </script
      
    <style>
        .red {
            color:red
        }
        .green {
            color:green
        }
    </style>
</head>
  
<body ng-app="app" style="text-align:center">
      
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h2 style="">ng-pattern Directive</h2>
  
    <div ng-controller="geek">
        <ng-form name="pass">
            Password:<input type="password" ng-model="password"
                name="password" ng-pattern="re" /><br>
                  
            <span ng-show="pass.password.$error.pattern" 
                    style="color:red">
                Not valid password.
            </span><br>
              
            Confirm: <input type="password" ng-model="repass"
                    ng-keyup="compare(repass)" name="repass"
                    ng-pattern="re" /><br>
              
            <span ng-show="isconfirm || pass.repass.$dirty "
                ng-class="{true:'green',false:'red'}[isconfirm]">
                Password {{isconfirm==true?'':'not'}} match
            </span>
        </ng-form>
    </div>
      
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope', function ($scope) {
            $scope.re = /^[a-zA-Z]\w{3,14}$/;
              
            $scope.compare = function (repass) {
                $scope.isconfirm = $scope.password == repass ?
                        true : false;
            }
        }]);
    </script>
</body>
  
</html>                    

Output:

  • Invalid Input:
    ngpattern
  • Input doesn’t Match:
    ngpattern
  • Valid Input:
    ngpattern

Example 2: This example shows error if the input is anything other than number.




<!DOCTYPE html>
<html>
  
<head>
    <title>ng-pattern Directive</title>
      
    <script src=
    </script
</head>
  
<body ng-app="app" style="text-align:center">
      
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h2 style="">ng-pattern Directive</h2>
  
    <div ng-controller="geek">
        <ng-form name="num">
            Input Number: <input type="text" ng-model="number"
                name="number" ng-pattern="re" /><br />
              
            <span ng-show="num.number.$error.pattern" style="color:red">
                Input is not valid.
            </span>
        </ng-form>
    </div>
      
    <script>
        var app = angular.module("app", []);
          
        app.controller('geek', ['$scope', function ($scope) {
            $scope.re = /^[0-9]{1,6}$/;
        }]);
    </script>
</body>
  
</html>                    

Output:

  • Input is text:
    ngpattern
  • Input is number:
    ngpattern



My Personal Notes arrow_drop_up
Recommended Articles
Page :