AngularJS | ng-pattern Directive
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:

- Input doesn’t Match:

- Valid Input:

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:

- Input is number:

Recommended Posts:
- AngularJS | ng-app Directive
- AngularJS | ng-if Directive
- AngularJS | ng-value Directive
- AngularJS | ng-cut Directive
- AngularJS | ng-src Directive
- AngularJS | ng-csp Directive
- AngularJS | Directive
- AngularJS | ng-jq Directive
- AngularJS | ng-selected Directive
- AngularJS | ng-readonly Directive
- AngularJS | ng-required Directive
- AngularJS | ng-paste Directive
- AngularJS | ng-srcset Directive
- AngularJS | ng-change Directive
- AngularJS | ng-blur Directive
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



