In this article I will explain with an example, how to perform US Mobile Number validation in AngularJS.
This article will illustrate how to use AngularJS Form validation for TextBox and ng-pattern directive and Regular Expressions (Regex) for validating US Mobile Number in AngularJS.
Validating US Mobile Number using ng-pattern and Regular Expressions in AngularJS
The following 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 Mobile Number validations and a HTML Button.
The Form
The HTML DIV consists of an HTML Form which has been created using following directives.
ng-submit – The ng-submit AngularJS directive which has been set with 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)
required – It is an HTML5 attribute which is used to specify that the TextBox cannot be kept empty during Form submission.
ng-pattern – The ng-pattern AngularJS directive is used to specify the Regular Expressions (Regex) pattern for validating the TextBox text.
The following conditions must satisfy for a US Mobile Number to be termed as valid.
1. It should be 10 digits long.
2. It may begin with an optional (.
3. After the optional (, it must be 3 digits. If it does not have a (, it must start with 3 digits.
4. It can have an optional ) after first 3 digits.
5. It can have an optional hyphen (-) or empty space after ), if present or after first 3 digits.
6. Then there must be 3 more digits.
7. After second set of 3 digits, it can have another optional hyphen (-) or empty space.
8. Finally, it must end with four digits.
Valid examples: (308)-135-7895, 308-135-7895, 308135-7895, 3081357895, (308) 135 7895, 308 135 7895, 308135 7895
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 MobileNumber TextBox is failing while the second HTML SPAN will be visible when the Regular Expressions (Regex) pattern validation for the MobileNumber 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.
$invalid – The Submit Button will be enabled only if the $invalid property of the Form is FALSE.
<script type="text/javascript" src=""></script>
<script type="text/javascript">
    var app = angular.module('MyApp', [])
    app.controller('MyController', function ($scope) { });
<div ng-app="MyApp" ng-controller="MyController">
    <form name="MyForm" ng-submit="MyForm.$valid" novalidate=novalidate>
        Mobile Number:
        <input type="text" ng-model="MobileNumber" name="MobileNumber" required=required
               ng-pattern="/^(\([0-9]{3}\)|[0-9]{3})[\s\-]?[\0-9]{3}[\s\-]?[0-9]{4}$/" />
        <span class="error" ng-show="MyForm.MobileNumber.$error.required">Mobile Number is required.</span>
        <span class="error" ng-show="MyForm.MobileNumber.$error.pattern">Invalid Mobile Number.</span>
        <button type="submit" ng-disabled="MyForm.$invalid">Submit</button>
CSS Class
The following CSS class is used for displaying the error message.
<style type="text/css">
    body { font-family: Arial; font-size: 10pt; }
    .error { color: Red; }
US Mobile Number validation using Regular Expressions in AngularJS
Browser Compatibility

The above code has been tested in the following browsers only in versions that support HTML5.

Internet Explorer  FireFox  Chrome  Safari  Opera 

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