How to validate PAN Card number using Regular Expression
Last Updated :
26 Dec, 2022
Given string str of alphanumeric characters, the task is to check whether the string is a valid PAN (Permanent Account Number) Card number or not by using Regular Expression . The valid PAN Card number must satisfy the following conditions:
It should be ten characters long.
The first five characters should be any upper case alphabets.
The next four-characters should be any number from 0 to 9.
The last(tenth) character should be any upper case alphabet.
It should not contain any white spaces.
Examples:
Input: str = “BNZAA2318J” Output: true Explanation: The given string satisfies all the above mentioned conditions.
Input: str = “23ZAABN18J” Output: false Explanation: The given string not starts with upper case alphabets, therefore it is not a valid PAN Card number.
Input: str = “BNZAA2318JM” Output: false Explanation: The given string contains eleven characters, therefore it is not a valid PAN Card number.
Input: str = “BNZAA23184” Output: true Explanation: The last(tenth) character of this string is a numeric(0-9) character, therefore it is not a valid PAN Card number.
Input: str = “BNZAA 23184” Output: true Explanation: The given string contains white spaces, therefore it is not a valid PAN Card number.
Approach: This problem can be solved by using Regular Expression.
Get the string.
Create a regular expression to validate the PAN Card number as mentioned below:
regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
Where:
[A-Z]{5} represents the first five upper case alphabets which can be A to Z.
[0-9]{4} represents the four numbers which can be 0-9.
[A-Z]{1} represents the one upper case alphabet which can be A to Z.
Match the given string with the regex, in Java, this can be done using Pattern.matcher() .
Return true if the string matches with the given regex, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidPanCardNo(string panCardNo)
{
const regex pattern("[A-Z]{5}[0-9]{4}[A-Z]{1}");
if (panCardNo.empty()) {
return false;
}
if (regex_match(panCardNo, pattern))
{
return true;
}
else
{
return false;
}
}
int main()
{
string str1 = "BNZAA2318J";
cout << isValidPanCardNo(str1) << endl;
string str2 = "23ZAABN18J";
cout << isValidPanCardNo(str2) << endl;
string str3 = "BNZAA2318JM";
cout << isValidPanCardNo(str3) << endl;
string str4 = "BNZAA23184";
cout << isValidPanCardNo(str4) << endl;
string str5 = "BNZAA 23184";
cout << isValidPanCardNo(str5) << endl;
return 0;
}
Java
import java.util.regex.*;
class GFG
{
public static boolean isValidPanCardNo(String panCardNo)
{
String regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
Pattern p = Pattern.compile(regex);
if (panCardNo == null)
{
return false;
}
Matcher m = p.matcher(panCardNo);
return m.matches();
}
public static void main(String args[])
{
String str1 = "BNZAA2318J";
System.out.println(isValidPanCardNo(str1));
String str2 = "23ZAABN18J";
System.out.println(isValidPanCardNo(str2));
String str3 = "BNZAA2318JM";
System.out.println(isValidPanCardNo(str3));
String str4 = "BNZAA23184";
System.out.println(isValidPanCardNo(str4));
String str5 = "BNZAA 23184";
System.out.println(isValidPanCardNo(str5));
}
}
Python3
import re
def isValidPanCardNo(panCardNo):
regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}"
p = re.compile(regex)
if(panCardNo == None):
return False
if(re.search(p, panCardNo) and
len(panCardNo) == 10):
return True
else:
return False
str1 = "BNZAA2318J"
print(isValidPanCardNo(str1))
str2 = "23ZAABN18J"
print(isValidPanCardNo(str2))
str3 = "BNZAA2318JM"
print(isValidPanCardNo(str3))
str4 = "BNZAA23184"
print(isValidPanCardNo(str4))
str5 = "BNZAA 23184"
print(isValidPanCardNo(str5))
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main(string[] args)
{
string[] str={"BNZAA2318J","23ZAABN18J","BNZAA2318JM","BNZAA23184","BNZAA 23184"};
foreach(string s in str) {
Console.WriteLine( isValidPanCardNo(s) ? "true" : "false");
}
Console.ReadKey(); }
public static bool isValidPanCardNo(string str)
{
string strRegex = @"[A-Z]{5}[0-9]{4}[A-Z]{1}$";
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return (true);
else
return (false);
}
}
Javascript
function isValidPanCardNo(panCardNo) {
let regex = new RegExp(/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/);
if (panCardNo == null) {
return "false";
}
if (regex.test(panCardNo) == true) {
return "true";
}
else {
return "false";
}
}
let str1 = "BOSPC9911H";
console.log(isValidPanCardNo(str1));
let str2 = "23ZAABN18J";
console.log(isValidPanCardNo(str2));
let str3 = "BNZAA2318JM";
console.log(isValidPanCardNo(str3));
let str4 = "BNZAA23184";
console.log(isValidPanCardNo(str4));
let str5 = "BNZAA 23184";
console.log(isValidPanCardNo(str5));
Output
true
false
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string. Auxiliary Space: O(1)
Share your thoughts in the comments
Add Your Comment
Please Login to comment...