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++
// C++ program to validate the
// PAN Card number using Regular
// Expression
#include <iostream>
#include <regex>
using namespace std;
// Function to validate the
// PAN Card number.
bool isValidPanCardNo(string panCardNo)
{
// Regex to check valid
// PAN Card number.
const regex pattern("[A-Z]{5}[0-9]{4}[A-Z]{1}");
// If the PAN Card number
// is empty return false
if (panCardNo.empty()) {
return false;
}
// Return true if the PAN Card number
// matched the ReGex
if (regex_match(panCardNo, pattern))
{
return true;
}
else
{
return false;
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "BNZAA2318J";
cout << isValidPanCardNo(str1) << endl;
// Test Case 2:
string str2 = "23ZAABN18J";
cout << isValidPanCardNo(str2) << endl;
// Test Case 3:
string str3 = "BNZAA2318JM";
cout << isValidPanCardNo(str3) << endl;
// Test Case 4:
string str4 = "BNZAA23184";
cout << isValidPanCardNo(str4) << endl;
// Test Case 5:
string str5 = "BNZAA 23184";
cout << isValidPanCardNo(str5) << endl;
return 0;
}
// This code is contributed by yuvraj_chandra
Java
// Java program to validate the
// PAN Card number using Regular Expression
import java.util.regex.*;
class GFG
{
// Function to validate the PAN Card number.
public static boolean isValidPanCardNo(String panCardNo)
{
// Regex to check valid PAN Card number.
String regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the PAN Card number
// is empty return false
if (panCardNo == null)
{
return false;
}
// Pattern class contains matcher() method
// to find matching between given
// PAN Card number using regular expression.
Matcher m = p.matcher(panCardNo);
// Return if the PAN Card number
// matched the ReGex
return m.matches();
}
// Driver Code.
public static void main(String args[])
{
// Test Case 1:
String str1 = "BNZAA2318J";
System.out.println(isValidPanCardNo(str1));
// Test Case 2:
String str2 = "23ZAABN18J";
System.out.println(isValidPanCardNo(str2));
// Test Case 3:
String str3 = "BNZAA2318JM";
System.out.println(isValidPanCardNo(str3));
// Test Case 4:
String str4 = "BNZAA23184";
System.out.println(isValidPanCardNo(str4));
// Test Case 5:
String str5 = "BNZAA 23184";
System.out.println(isValidPanCardNo(str5));
}
}
Python3
# Python3 program to validate the
# PAN Card number using Regular
# Expression
import re
# Function to validate the
# PAN Card number.
def isValidPanCardNo(panCardNo):
# Regex to check valid
# PAN Card number
regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}"
# Compile the ReGex
p = re.compile(regex)
# If the PAN Card number
# is empty return false
if(panCardNo == None):
return False
# Return if the PAN Card number
# matched the ReGex
if(re.search(p, panCardNo) and
len(panCardNo) == 10):
return True
else:
return False
# Driver Code.
# Test Case 1:
str1 = "BNZAA2318J"
print(isValidPanCardNo(str1))
# Test Case 2:
str2 = "23ZAABN18J"
print(isValidPanCardNo(str2))
# Test Case 3:
str3 = "BNZAA2318JM"
print(isValidPanCardNo(str3))
# Test Case 4:
str4 = "BNZAA23184"
print(isValidPanCardNo(str4))
# Test Case 5:
str5 = "BNZAA 23184"
print(isValidPanCardNo(str5))
# This code is contributed by avanitrachhadiya2155
C#
// C# program to validate the
// PAN Card numbe
//using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
// Main Method
static void Main(string[] args)
{
// Input strings to Match
// PAN Card numbe
string[] str={"BNZAA2318J","23ZAABN18J","BNZAA2318JM","BNZAA23184","BNZAA 23184"};
foreach(string s in str) {
Console.WriteLine( isValidPanCardNo(s) ? "true" : "false");
}
Console.ReadKey(); }
// method containing the regex
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);
}
}
// This code is contributed by Rahul Chauhan
JavaScript
// Javascript program to validate
// PAN Number using Regular Expression
// Function to validate the
// PAN Number
function isValidPanCardNo(panCardNo) {
// Regex to check valid
// PAN Number
let regex = new RegExp(/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/);
// if PAN Number
// is empty return false
if (panCardNo == null) {
return "false";
}
// Return true if the PAN NUMBER
// matched the ReGex
if (regex.test(panCardNo) == true) {
return "true";
}
else {
return "false";
}
}
// Driver Code
// Test Case 1:
let str1 = "BOSPC9911H";
console.log(isValidPanCardNo(str1));
// Test Case 2:
let str2 = "23ZAABN18J";
console.log(isValidPanCardNo(str2));
// Test Case 3:
let str3 = "BNZAA2318JM";
console.log(isValidPanCardNo(str3));
// Test Case 4:
let str4 = "BNZAA23184";
console.log(isValidPanCardNo(str4));
// Test Case 5:
let str5 = "BNZAA 23184";
console.log(isValidPanCardNo(str5));
// This code is contributed by Rahul Chauhan
Outputtrue
false
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Similar Reads
How to validate Visa Card number using Regular Expression Given a string str, the task is to check whether the given string is a valid Visa Card number or not by using Regular Expression. The valid Visa Card number must satisfy the following conditions: It should be 13 or 16 digits long, new cards have 16 digits and old cards have 13 digits.It should start
6 min read
How to validate CVV number using Regular Expression Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not ha
5 min read
How to validate MAC address using Regular Expression Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with
6 min read
How to validate Indian Passport number using Regular Expression 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 uppe
5 min read
How to validate pin code of India using Regular Expression Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod
6 min read
How to validate IFSC Code using Regular Expression Given string str, the task is to check whether the given string is a valid IFSC (Indian Financial System) Code or not by using Regular Expression. The valid IFSC (Indian Financial System) Code must satisfy the following conditions: It should be 11 characters long.The first four characters should be
8 min read
How to validate MasterCard number using Regular Expression Given string str, the task is to check whether the given string is a valid Master Card number or not by using Regular Expression. The valid Master Card number must satisfy the following conditions. It should be 16 digits long.It should start with either two digits numbers may range from 51 to 55 or
7 min read
How to validate ISIN using Regular Expressions ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati
6 min read
How to validate an IP address using Regular Expressions in Java Given an IP address, the task is to validate this IP address with the help of Regular Expressions.The IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Examples: Inpu
3 min read
How to validate a Username using Regular Expressions in Java Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or
3 min read