In this article I will explain with an example, how to perform Passport Number validation in AngularJS.
Validating Passport 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.
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 Passport 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.
The following conditions must satisfy for an Indian Passport Number to be termed as valid.
1. It should be eight characters long.
2. The first character should be an upper case alphabet with A-Z excluding Q, X, and Z.
3. The second character should be any digit. 1-9.
4. The third character should be any digit. 0-9.
5. The next character should be zero or one white space character.
6. The next four characters should be any digit. 0-9.
7. The last character should be any digit. 1-9.
Examples: A2096457, A20 96457
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 PassportNumber TextBox is failing while the second HTML SPAN will be visible when the Regular Expressions (Regex) pattern validation for the PassportNumber 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>
Passport Number:
<input type="text" ng-model="PassportNumber" name="PassportNumber" required=required
ng-pattern="/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/" class="passport" />
<span class="error" ng-show="MyForm.PassportNumber.$error.required">Passport Number is required.</span>
<span class="error" ng-show="MyForm.PassportNumber.$error.pattern">Invalid Passport Number.</span>
<hr/>
<button type="submit" ng-disabled="MyForm.$invalid">Submit</button>
</form>
</div>
CSS Class
The following CSS classes are used.
1. error – It will apply RED color to the error message.
2. passport – It will force the letters to be UPPER case.
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
.error { color: Red; }
.passport { text-transform: uppercase; }
</style>
Screenshot
Browser Compatibility
The above code has been tested in the following browsers only in versions that support HTML5.
* All browser logos displayed above are property of their respective owners.
Demo
Downloads