Validate a PAN Card number in Java using Regular Expression

A Regular Expression is a sequence of characters that define a search pattern, usually used for searching and replacing patterns in strings. Many different programming languages support Regular Expressions, popularly called regex. Java supports Regular Expressions with its util.regex package. This article explains how to validate a PAN number using Regular Expression in Java with an example.

A Regular Expression for a valid PAN card number

A PAN card number will have exactly 10 characters, only containing numbers 0-9 and upper case alphabets A-Z. Any PAN number will have the following pattern:

  1. Five upper case alphabets [A-Z] occupying first five positions, 1-5
  2. Four numbers [0-9] occupying next four positions, 6-9
  3. An upper case alphabet [A-Z] in the last position, 10

Using this pattern, a regular expression can be formed and used to validate whether or not a PAN number is valid. A regular expression for the above pattern would be

[A-Z]{5}[0-9]{4}[A-Z]{1}

In Java, a class called Pattern has the methods required for defining and using Regular Expressions for pattern searching in strings.

An Example of validating PAN card using Regex in Java

The following example uses the Pattern class of util.regex to validate a PAN Card number.

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.Scanner;
class PANCardValidator {
    String panCardPattern;
    public PANCardValidator() {
        panCardPattern = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
    }
    public boolean validate (String panCardNumber) {
        try {
            if (Pattern.matches(panCardPattern, panCardNumber) == true)
                return true;
            else
                return false;
        }
        catch (PatternSyntaxException e) {
            e.printStackTrace();
            return false;
        }
    }
}

class PANExample {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PANCardValidator validator = new PANCardValidator();
        System.out.println("Enter PAN Number: ");
        String PAN = in.next();
        boolean validStatus = validator.validate(PAN);
        if (validStatus == true)
            System.out.println(PAN+" is a valid PAN number");
        else
            System.out.println(PAN+" is not a valid PAN number");        
        in.close();    
    }
}

The method matches() of the class Pattern has the signature,

public static boolean matches(String regex, CharSequence input)

The method matches() of class the Pattern accepts the regular expression to be compiled and the input to be validated against the regular expression as inputs and returns whether or not the input matches the regular expression. This method might throw PatternSyntaxException if the regular expression syntax is invalid. So it is better to wrap the method in a try...catch block. Invoking this method on the class Pattern is similar to compiling a regular expression using the Pattern.compile() method, then creating a matcher using Pattern.matcher() method and then invoking the method Matcher.matches(). i.e,

Pattern.compile(regex).matcher(input).matches()

A pattern can be compiled to a Pattern object using Pattern.compile() if that pattern will be used many times rather than invoking the Pattern.matches() every time. Thus efficiency can be improved.

When the above example is run, it produces the following output

Enter PAN Number:
BACBA1256D
BACBA1256D is a valid PAN number
Enter PAN Number:
ASD1234C6
ASD1234C6 is not a valid PAN number

Leave a Reply

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