How to validate Indian Passport number using Regular Expression
Last Updated :
13 Aug, 2024
Given a string str of alphanumeric characters, the task is to check whether the given string is a valid passport number or not by using Regular Expression.
A valid passport number in India must satisfy the following conditions:
- It should be eight characters long.
- The first character should be an uppercase alphabet.
- The next two characters should be a number, but the first character should be any number from 1-9 and the second character should be any number from 0-9.
- It should be zero or one white space character.
- The next four characters should be any number from 0-9.
- The last character should be any number from 1-9.
Exapmles:
Input: str = "A2096457"; Output: true Explanation: The given string satisfies all the above mentioned conditions. Therefore it is a valid passport number of India. Input: str = "12096457"; Output: false Explanation: The given string doesn't starts with an upper case alphabet. Therefore it is not a valid passport number of India. Input: str = "A209645704"; Output: false Explanation: The given string contains 10 characters. Therefore it is not a valid passport number of India.
Approach:
The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.
- Get the String.
- Create a regular expression to check valid passport number of India as mentioned below:
- regex = "
^[A-Z][1-9]\d\s?\d{4}[1-9]$
" Where: ^
: Asserts the position at the start of the string. [A-Z]
: Ensures the first character is an uppercase letter from A to Z, which includes Q
and X
(since these are not excluded in your revised requirement).[1-9]
: The second character must be a digit from 1 to 9.\d
: The third character can be any digit from 0 to 9.\s?
: There can be zero or one whitespace character.\d{4}
: The next four characters must be digits from 0 to 9.[1-9]
: The last character must be a digit from 1 to 9.$
: Asserts the position at the end of the string.
- Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
// Function to validate the passport number
bool isValidPassportNo(const string& str) {
// Regex to check valid passport number
const regex pattern("^[A-Z][1-9]\\d\\s?\\d{4}[1-9]$");
// Return false if the passport number is empty
if (str.empty()) {
return false;
}
// Return true if the passport number matches the regex pattern
return regex_match(str, pattern);
}
// Driver Code
int main() {
// Test Case 1:
string str1 = "A21 90457";
cout << (isValidPassportNo(str1) ? "true" : "false") << endl;
// Test Case 2:
string str2 = "A0296457";
cout << (isValidPassportNo(str2) ? "true" : "false") << endl;
// Test Case 3:
string str3 = "Q2096453";
cout << (isValidPassportNo(str3) ? "true" : "false") << endl;
// Test Case 4:
string str4 = "12096457";
cout << (isValidPassportNo(str4) ? "true" : "false") << endl;
// Test Case 5:
string str5 = "A209645704";
cout << (isValidPassportNo(str5) ? "true" : "false") << endl;
return 0;
}
Java
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class PassportValidator {
// Function to validate the passport number
public static boolean isValidPassportNo(String str) {
// Regex to check valid passport number
// The pattern ensures:
// - Starts with an uppercase letter (A-Z)
// - Followed by a digit between 1-9
// - Followed by any digit (0-9)
// - Optional single whitespace
// - Followed by exactly four digits (0-9)
// - Ends with a digit between 1-9
String regex = "^[A-Z][1-9]\\d\\s?\\d{4}[1-9]$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
// Return true if the passport number matches the regex
return matcher.matches();
}
public static void main(String[] args) {
// Test Case 1:
// Valid format (starts with an uppercase letter, correct length, optional space, ends with a digit)
String str1 = "A21 90457";
System.out.println(isValidPassportNo(str1));
// Test Case 2:
// Valid format (starts with an uppercase letter, correct length, no space, ends with a digit)
String str2 = "A0296457";
System.out.println(isValidPassportNo(str2));
// Test Case 3:
// Valid format (starts with an uppercase letter, correct length, no space, ends with a digit)
String str3 = "Q2096453";
System.out.println(isValidPassportNo(str3));
// Test Case 4:
// Invalid (does not start with an uppercase letter)
String str4 = "12096457";
System.out.println(isValidPassportNo(str4));
// Test Case 5:
// Invalid (length is not 8 characters)
String str5 = "A209645704";
System.out.println(isValidPassportNo(str5));
}
}
Python
import re
def is_valid_passport_no(s):
# Regex to check valid passport number
pattern = r'^[A-Z][1-9]\d\s?\d{4}[1-9]$'
# Return true if the passport number matches the regex
return bool(re.match(pattern, s))
# Test Cases
print(is_valid_passport_no("A21 90457"))
print(is_valid_passport_no("A0296457"))
print(is_valid_passport_no("Q2096453"))
print(is_valid_passport_no("12096457"))
print(is_valid_passport_no("A209645704"))
C#
using System;
using System.Text.RegularExpressions;
class Program
{
// Function to validate the passport number
static bool IsValidPassportNo(string str)
{
// Regex to check valid passport number
string pattern = @"^[A-Z][1-9]\d\s?\d{4}[1-9]$";
Regex regex = new Regex(pattern);
// Return true if the passport number matches the regex
return regex.IsMatch(str);
}
static void Main()
{
// Test Case 1:
string str1 = "A21 90457"; // Valid format
Console.WriteLine(IsValidPassportNo(str1));
// Test Case 2:
string str2 = "A0296457"; // Valid format (no space)
Console.WriteLine(IsValidPassportNo(str2));
// Test Case 3:
string str3 = "Q2096453"; // Valid format
Console.WriteLine(IsValidPassportNo(str3));
// Test Case 4:
string str4 = "12096457"; // Invalid (does not start with a letter)
Console.WriteLine(IsValidPassportNo(str4));
// Test Case 5:
string str5 = "A209645704";
Console.WriteLine(IsValidPassportNo(str5));
}
}
JavaScript
function isValidPassportNo(str) {
// Regex to check valid passport number
const pattern = /^[A-Z][1-9]\d\s?\d{4}[1-9]$/;
// Return true if the passport number matches the regex
return pattern.test(str);
}
// Test Cases
console.log(isValidPassportNo("A21 90457"));
console.log(isValidPassportNo("A0296457"));
console.log(isValidPassportNo("Q2096453"));
console.log(isValidPassportNo("12096457"));
console.log(isValidPassportNo("A209645704"));
Outputtrue
false
true
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)