In this article I will explain with an example, how to perform Age range validation (Number range validation) in AngularJS.
The Age range i.e. 18 to 60 years will be validated using AngularJS Form Validation for TextBox and ng-pattern directive in AngularJS.
 
 
Validating Age (Number) using ng-pattern and Regular Expressions using 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 Age (Number) range validations and a HTML Button.
Form
ng-submit - The HTML Form has been assigned ng-submitAngularJS 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 Age (Number) TextBox is failing while the second HTML SPAN will be visible when the Regular Expressions (Regex) pattern validation for the Age (Number) 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>
    <table border="0" cellpadding="0" cellspacing="5">
        <tr>
            <td>
                Age:
            </td>
            <td>
                <input type="text" ng-model="Age" name="Age" required ng-pattern="/^(?:1[8-9]|[2-5][0-9]|60)$/" />
            </td>
            <td>
                <span class="error" ng-show="MyForm.Age.$error.required">*Required</span> <span
                    class="error" ng-show="MyForm.Age.$error.pattern">*Invalid Age. Valid 18-60</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: Validate Age (Number) Range using Regular Expressions
 
 
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