In this article I will explain a simple tutorial with an example, how to use the ng-pattern directive in AngularJS.
The ng-pattern directive is used to validate the user input for the invalid characters with the help of Regular Expression patterns in AngularJS.
 
 
Using ng-pattern with Regular Expressions in AngularJS
The below HTML Markup consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned.
Note: If you want to learn more about these directives, please refer my article Introduction to AngularJS.
 
The AngularJS app HTML DIV consists of an HTML Form with a TextBox to be validated, two HTML SPAN elements for displaying the error message for Required and Email validations and a HTML Button.
Form
ng-submit – The HTML Form has been assigned ng-submit AngularJS directive which has the expression MyForm.$valid which means the Form will be submitted only if the $valid property of the Form is TRUE.
novalidate – It is an HTML5 attribute which directs the Browser to disable the HTML5 validations for the particular Form.
 
TextBox (INPUT)
ng-pattern – The ng-pattern AngularJS directive is used to specify the Regular Expressions (Regex) pattern for validating the TextBox text.
required – It is an HTML5 attribute which is used to specify that the TextBox cannot be kept empty during Form submission.
 
SPAN
ng-show – The ng-show AngularJS directive will make the HTML SPAN visible only when the expression to be tested returns TRUE.
The first HTML SPAN will be visible when the Required validation for the Email TextBox is failing while the second HTML SPAN will be visible when the Regular Expressions (Regex) pattern validation for the Email TextBox is failing.
 
Submit Button
ng-disabled – The ng-disabled AngularJS directive will disable the Submit Button and enable only when the expression to be tested returns TRUE.
The Submit Button will be enabled only if the $valid property of the Form is TRUE.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module('MyApp', [])
    app.controller('MyController', function ($scope) {
    });
</script>
<div ng-app="MyApp" ng-controller="MyController">
    <form name="MyForm" ng-submit="MyForm.$valid" novalidate>
    <table border="0" cellpadding="0" cellspacing="5">
        <tr>
            <td>
                Email:
            </td>
            <td>
                <input type="text" ng-model="EmailAddress" name="Email" required ng-pattern="/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/" />
            </td>
            <td>
                <span class="error" ng-show="MyForm.Email.$error.required">*Required</span> <span
                    class="error" ng-show="MyForm.Email.$error.pattern">*Invalid Email Address</span>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <hr />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <button type="submit" ng-disabled="MyForm.$invalid">Submit</button>
            </td>
        </tr>
    </table>
    </form>
</div>
 
 
Screenshot
AngularJS: ng-pattern Tutorial with example
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Demo
 
 
Downloads