How to validate Indian Passport number using Regular Expression?


An Indian passport number is special alphanumeric code that Indian government issues to owner of an Indian passport. The passport number is made up of 8–12 characters, that may include both letters and digits. The first two characters of passport number indicate type of passport, such as P for an ordinary passport, D for a diplomatic passport, and S for an official passport. The next two characters stand for the code of the organization that issues passports, and they are followed by string of numbers that serve as passport holder's special identification. Indian passport numbers are normally printed on the passport's biographical information page and are used for identification when travelling abroad.

Method

The methods below can be used to validate an Indian passport number using regular expressions −

  • Recognise the format of an Indian passport number

  • Validate regular expression pattern

Method 1: Validate regular expression pattern

After creating this regular expression pattern, we can validate it using sample set of Indian passport numbers to make sure it adheres to format that is required.

Syntax

// Define the Regular Expression pattern for an Indian passport number
regex pattern("[PDS][A-Z]{2}\d{4}[A-Z0-9]{2}");

// Compile the Regular Expression pattern
regex passport_regex(pattern);

// Test the validity of the Indian passport number
string passport_number = "P1234A5678";
bool is_valid_passport = regex_match(passport_number, passport_regex);

Algorithm

The following algorithm shows how to use regular expressions to verify an Indian passport number −

Step 1 − Determine the regular expression pattern that corresponds to Indian passport numbers an authentic Indian passport number is made up of eight or nine characters, which may include both letters and digits. A letter appears as the first character, then a six-digit number, then either a letter or a number concludes the string.

Step 2 − Utilise regular expression engine to the compile pattern of regular expression. It will generate an object for regular expression which may be used to compare strings to pattern.

Step 3 − Select string from the user input that corresponds to an Indian Passport number.

Step 4 − Using the regular expression object established in step 2, apply the regular expression pattern to the input string. This will determine whether the input string fits the pattern.

Step 5 − If the pattern and the input string match, the passport number is valid. The input string must match the pattern exactly for the passport number to be legitimate.

Example 1

The validate Passport Number () function in this illustration accepts a string parameter representing the Indian passport number and returns a Boolean value indicating whether the string matches regular expression pattern. The string that regular expression pattern matches is one capital letter followed by seven numbers, and it is defined as "[A-Z]1[0-9]7$". To compare the passport number to the regular expression pattern, use the regex_match () function. The function returns true, meaning that the passport number is legitimate, if it matches the pattern. If the passport number is invalid, it returns false and returns false otherwise. In order to conform an Indian passport number, the accurate Passport Number method is used in the main () function.

#include <iostream>
#include <regex>
#include <string>

using namespace std;

bool validatePassportNumber(string passportNumber) {
   regex pattern("^[A-Z]{1}[0-9]{7}$");
   // The pattern shows the format of the Indian passport number, here the first character is a capital letter and the next 7 characters are digits.

   if (regex_match(passportNumber, pattern)) {
      return true;
   }
   return false;
}

int main() {
   string passportNumber = "A1234567"; // Indian Passport Number to be validated
   if (validatePassportNumber(passportNumber)) {
      cout << "Valid Indian Passport Number" << endl;
   }
   else {
      cout << "Invalid Indian Passport Number" << endl;
   }
   return 0;
}

Output

Valid Indian Passport Number

Method 2. Recognise the format of an Indian passport number

It is made up of 8 to 12 characters, which can include both letters and digits. The type of passport is indicated by the first two characters, which are then followed by a string of digits and passport issuing agency's code. Syntax and algorithm will be same for both of the methods.

Example 2

Sure! The following snippet of C++ code illustrates how to use regular expressions to validate the Indian passport number −

In this example, we define pattern for a regular expression that corresponds to format of an Indian passport number. The regex_match () function from the "regex" package is then used to test pattern against a few sample passport numbers. The console is printed with validation results.

The passport numbers are hardcoded as strings in code in this example, so take note, there is no user input present. In a practical application, you would probably get passport number through user input or another outside source.

#include <iostream>
#include <regex>

using namespace std;

int main() {
   // Define a regular expression pattern for Indian Passport numbers
   regex pattern("^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$");

   // Test some example passport numbers
   string passport1 = "Z1234567";   // Invalid
   string passport2 = "A12345678";  // Invalid
   string passport3 = "P1234567";   // Valid
   string passport4 = "M123 4567";  // Valid

   // Validate the passport numbers using the regular expression pattern
   bool valid1 = regex_match(passport1, pattern);
   bool valid2 = regex_match(passport2, pattern);
   bool valid3 = regex_match(passport3, pattern);
   bool valid4 = regex_match(passport4, pattern);

   // Print the validation results
   cout << "Passport 1 is " << (valid1 ? "valid" : "invalid") << endl;
   cout << "Passport 2 is " << (valid2 ? "valid" : "invalid") << endl;
   cout << "Passport 3 is " << (valid3 ? "valid" : "invalid") << endl;
   cout << "Passport 4 is " << (valid4 ? "valid" : "invalid") << endl;

   return 0;
}

Output

Passport 1 is invalid
Passport 2 is invalid
Passport 3 is invalid
Passport 4 is invalid

Conclusion

In conclusion, a regular phrase can be used to verify an Indian passport number. The format and structure of the Indian passport number, also including the type of passport, the passport issuing authority code, and the identification number for the passport bearer, can be checked using a regular expression. Regular expressions may be used to authenticate Indian passport numbers rapidly and reliably, which is useful in a several of circumstances like visa applications and immigration procedures.

Updated on: 31-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements