Introduction
In this blog, let's discuss how to validate a form with AngularJS ng-pattern using regular expressions.
Background
Most of the times, we have to create form with validate input fields. Then we require to write code in JavaScript or server-side code for validation .This blog will show you how to achieve validation with reg-ex very easily.
Below, I have created a demostration example.
Step 1

We create HTML file and include reference of AngularJS file from CDN.
Step 2

We create ng-app, ng-controller, and inject module.
Step 3

We create Controller in JavaScript .
Step 4

We use ng-pattern for giving regular expression.
Step 5

We create span or div for hiding and showing when user input is $dirty or $invalid value.
HTML Code Snippet
  1. <html ng-app="myApp">
  2. <head>
  3. <title>ng-pattern with regx</title>
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
  5. </head>
  6. <body>
  7. <h1 style="text-align:center">Validation Example</h1>
  8. <div ng-controller="cntrl" style="margin-left:500px">
  9. <form name="SaveForm" ng-submit="funcSave()">
  10. <table border="1" cellspacing="0" cellpadding="10">
  11. <tr>
  12. <td>Name</td>
  13. <td><input type="text" required maxlength="60" ng-pattern="/^[a-zA-Z. ]*[a-zA-Z]{1,60}$/" name="name" ng-model="name" />
  14. <br/>
  15. <span style="color:red" ng-show="SaveForm.name.$dirty && SaveForm.name.$invalid" class="ng-hide">
  16. Please Enter Valid Name.!
  17. </span>
  18. </td>
  19. </tr>
  20. <tr>
  21. <td>Date Of Birth (dd/mm/yyyy)</td>
  22. <td><input type="text" required ng-pattern="/^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/" ng-model="dob" name="dob" />
  23. <br/>
  24. <span style="color:red" ng-show="SaveForm.dob.$dirty && SaveForm.dob.$invalid" class="ng-hide">
  25. Please Enter Valid Date Of Birth.!
  26. </span>
  27. </td>
  28. </tr>
  29. <tr>
  30. <td>Email</td>
  31. <td><input type="text" maxlength="100" required ng-pattern="/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/" name="email" ng-model="email" />
  32. <br/>
  33. <span style="color:red" ng-show="SaveForm.email.$dirty && SaveForm.email.$invalid" class="ng-hide">
  34. Please Enter Valid Email.!
  35. </span>
  36. </td>
  37. </tr>
  38. <tr>
  39. <td>Mobile No </td>
  40. <td><input type="text" required maxlength="10" ng-pattern="/^[7-9][0-9]{9}$/" name="mobileno" ng-model="mobileno" />
  41. <br/>
  42. <span style="color:red" ng-show="SaveForm.mobileno.$dirty && SaveForm.mobileno.$invalid" class="ng-hide">
  43. Please Enter Valid Mobile No.!
  44. </span>
  45. </td>
  46. </tr>
  47. <tr>
  48. <td>Pin code </td>
  49. <td><input type="text" required maxlength="6" ng-pattern="/^[1-9][0-9]{5}$/" name="pincode" ng-model="pincode" />
  50. <br/>
  51. <span style="color:red" ng-show="SaveForm.pincode.$dirty && SaveForm.pincode.$invalid" class="ng-hide">
  52. Please Enter Valid Pin.!
  53. </span>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td colspan="4" style="text-align:center"><input type="submit" value="submit" /></td>
  58. </tr>
  59. </table>
  60. </form>
  61. </div>
  62. </body>
  63. </html>
JavaScript Code Snippet
  1. <script>
  2. var app = angular.module("myApp", []);
  3. app.controller('cntrl',function($scope){
  4. $scope.funcSave = function()
  5. {
  6. if($scope.SaveForm.$valid) {
  7. alert("Form is Valid..!!");
  8. }
  9. else
  10. {
  11. alert("Form is not Valid..!!");
  12. }
  13. }
  14. })
  15. </script>
Output Screenshots

Note

One thing that you should keep in mind is - ng-pattern given name tag is required in input box and textarea. You can also create common JSON file for all forms accessing regx and given value in $rootscope.
Summary
I hope, you understood how to use regular expression in AngularJS ng-pattern.