Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression validating PAN card number

Tags:

regex

android

I have seen this question and this blog for the PAN regex. [A-Z]{5}[0-9]{4}[A-Z]{1}. But my question is a little bit more extended than that.

In a PAN card number:

1) The first three letters are sequence of alphabets from AAA to zzz
2) The fourth character informs about the type of holder of the Card. Each assesse is unique:`

    C — Company
    P — Person
    H — HUF(Hindu Undivided Family)
    F — Firm
    A — Association of Persons (AOP)
    T — AOP (Trust)
    B — Body of Individuals (BOI)
    L — Local Authority
    J — Artificial Judicial Person
    G — Government


3) The fifth character of the PAN is the first character
    (a) of the surname / last name of the person, in the case of 
a "Personal" PAN card, where the fourth character is "P" or
    (b) of the name of the Entity/ Trust/ Society/ Organisation
in the case of Company/ HUF/ Firm/ AOP/ BOI/ Local Authority/ Artificial Jurdical Person/ Govt,
where the fourth character is "C","H","F","A","T","B","L","J","G".

4) The last character is a alphabetic check digit.

I want the regex to be checking on the basis of that. Since I get Name of the person, or the Organization in another EditText, I needed to further verify the 4th and 5th letter.

It turns out to be [A-Z]{3}[C,H,F,A,T,B,L,J,G,P]{1}**something for the fifth character**[0-9]{4}[A-Z]{1}

I'm not able to figure out how that something has to be written.

Programmatically, it can be done, someone has done it in rails but can it be done through regex? How?

like image 391
inquisitive Avatar asked May 27 '15 04:05

inquisitive


People also ask

How can I verify my PAN number is valid?

Step 1: Go to e-Filing portal homepage. Step 2: Click Verify Your PAN on the e-Filing homepage. Step 3: On the Verify Your PAN page, enter your PAN, Full Name, Date of Birth and Mobile Number (accessible to you) and click Continue.

How do I validate a PAN number in Python?

import re def isValid(Z): Result=re. compile("[A-Za-z]{5}\d{4}[A-Za-z]{1}") return Result. match(Z) # Driver Code Z="ABCDE9999K" if (isValid(Z)): print ("It's a Valid PAN Number") else : print ("Invalid PAN Number entered.")

What is a valid PAN number?

PAN is a ten-digit unique alphanumeric number issued by the Income Tax Department. PAN is issued in the form of a laminated plastic card (commonly known as PAN card). Last character, i.e., the tenth character is an alphabetic check digit. Thus, option (c) is the correct option.


2 Answers

The regex you can use with matches() is formed based on the additional input from the users, and look-behinds check for the preceding 4th character. If the 4th letter is P, we check for the first letter in the surname, and if the 4th letter is not P, we check the first letter in the entity name:

String rx = "[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]";

Sample code:

String c1 = "S"; // First letter in surname coming from the EditText (with P before)
String c2 = "F"; // First letter in name coming from another EditText (not with P before)
String pan = "AWSPS1234Z"; // true
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCF1234Z"; // true
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCS1234Z"; // false
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
like image 158
Wiktor Stribiżew Avatar answered Sep 24 '22 22:09

Wiktor Stribiżew


enter image description here

Pan= edittextPan.getText().toString().trim();

Pattern pattern = Pattern.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}");

Matcher matcher = pattern .matcher(Pan);

if (matcher .matches()) {
Toast.makeText(getApplicationContext(), Pan+" is Matching",
 Toast.LENGTH_LONG).show();

}
else
{  
Toast.makeText(getApplicationContext(), Pan+" is Not Matching",
 Toast.LENGTH_LONG).show();
}
like image 40
Dhina k Avatar answered Sep 25 '22 22:09

Dhina k