Java RegEx – PAN Card Validation

Java RegEx – PAN Card Validation example shows how to validate PAN Card (Permanent Account Number) using regular expression pattern in Java.

How to validate PAN Card using regex in Java?

According to Wikipedia, the format of the PAN card number is as follows.

  1. 10 alpha-numeric characters in length. All alphabets are in upper case.
  2. The first five characters are letters, the next four characters are digits, and the last character is again a letter.
  3. The fourth character denotes the type of the cardholder and it must be one of the A, B, C, F, G, H, L, J, P, or T characters.

Let’s translate this requirement into the regex pattern one by one. The total length of the PAN number needs to be 10 characters long. The first five characters are letters, the next four are digits, and the last is a character. All characters need to be in uppercase.

In the first five characters, the fourth character needs to be any one of theĀ A, B, C, F, G, H, L, J, P, or T characters.

Considering these requirements, I’ve come up with the below given regex pattern.

Where,

Let’s test our regex pattern to validate some sample PAN card numbers.

Output

The first number “ABC123” failed because it is not 10 in length. The second number “1234ABCD12” starts with numbers so failed as well.

The third number “AAAYX0000A” does not have the fourth character from the set of “ABCFGHLJPT” so it is invalid as well. The fifth number “AAAGY000AA” does not have 4 digits after the 5th character, so it fails as well.

The sixth number “AAAGY00000” does not end with the character so it is an invalid PAN number. Finally, the last one “AAAGY0000A” passes our test as it confirms all the requirements of a valid PAN card number.

Additional Validation for a PAN card number (ONLY if you have cardholder name):

Although the above validation should be sufficient for any general use case, according to Wikipedia, the fifth character of the PAN card is the first character of the person’s last name or entity name. I have not included this requirement into the regex pattern due to the lack of the data you want to validate against.

If you have that data and want to include this check as well, you can replace the “[A-Z]” pattern for the fifth character with the first character of the person’s last name or an entity name. For example, if the cardholder’s last name is “Chopra”, the regex pattern will be like,

This example is a part of the Java Regular expression tutorial.

Please let me know your views in the comments section below.

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *