In this article I will explain with an example, how to implement Indian Passport Number validation using RegularExpressionValidator in ASP.Net.
The Passport Number entered in the TextBox will be validated using Regular Expression (Regex) and ASP.Net RegularExpressionValidator.
 
 
HTML Markup
The following HTML Markup consists of a TextBox, an ASP.Net RegularExpressionValidator and a Button control.
The RegularExpressionValidator has been set with a Regular Expression for validating Passport Number in the ValidationExpression property.
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
Passport Number:
<asp:TextBox ID="txtPassportNumber" runat="server" CssClass="passport" />
<asp:RegularExpressionValidator ID="rgxPassportNumber" runat="server" ControlToValidate="txtPassportNumber"
    ValidationExpression="[A-PR-WY][1-9]\d\s?\d{4}[1-9]$" ErrorMessage="Invalid Passport Number." ForeColor="Red">
</asp:RegularExpressionValidator>
<hr/>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
 
 
CSS Class
The following CSS class is used.
passport – It will force the letters to be UPPER case.
<style type="text/css">
    body { font-family: Arial; font-size: 10pt; }
    .passport { text-transform: uppercase; }
</style>
 
 
Screenshot
Passport Number validation using RegularExpressionValidator in ASP.Net
 
 
Demo
 
 
Downloads