In this article I will explain with an example, how to perform Indian 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 Indian Mobile Number in AngularJS.
 
 
Validating 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 Mobile Number to be termed as valid.
1. It should be 10 digits long.
2. The first digit should be a number. 6 to 9.
3. The rest 9 digits should be any number. 0 to 9.
4. It can have 11 digits including 0 at the starting.
5. It can have 12 digits including 91 at the starting.
Examples: 9876543210, 09876543210, 919876543210
 
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 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="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=novalidate>
        Mobile Number:
        <input type="text" ng-model="MobileNumber" name="MobileNumber" required="required"
               ng-pattern="/^(0|91)?[6-9][0-9]{9}$/" />
        <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>
        <hr/>
        <button type="submit" ng-disabled="MyForm.$invalid">Submit</button>
    </form>
</div>
 
 
CSS Class
The following CSS class is used.
error – It will apply RED color to the error message.
<style type="text/css">
    body { font-family: Arial; font-size: 10pt; }
    .error { color: Red; }
</style>
 
 
Screenshot
Indian 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.

 
 
Demo
 
 
Downloads