How to check the validation of edittext for pan card like "ABCDE1234F". I am confused how to check the the validation for this. Please help me guys. I will appreciate any kind of help.
16 Answers
You can use Regular Expression with pattern matching
String s = "ABCDE1234F"; // get your editext value here
Pattern pattern = Pattern.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}");
Matcher matcher = pattern.matcher(s);
// Check if pattern matches
if (matcher.matches()) {
Log.i("Matching","Yes");
}
// [A-Z]{5} - match five literals which can be A to Z
// [0-9]{4} - followed by 4 numbers 0 to 9
// [A-Z]{1} - followed by one literal which can A to Z
You can test regex @
http://java-regex-tester.appspot.com/
http://docs.oracle.com/javase/tutorial/essential/regex/
Update
Another anser that is complete Regular expression validating PAN card number the 5th char depends on 4th char.

- 17,741
- 7
- 42
- 75

- 132,755
- 26
- 225
- 256
-
1The first three letters are AAA-ZZZ. The Fourth letter is either of "C","H","F","A","T","B","L","J","G","P" This check can also be included, but based on the fourth letter, the fifth letter is determined if the fourth letter is P, the fifth letter is the first letter of last name of surname, and the for all others it the first letter of the name. How can we make such a regex? – inquisitive May 26 '15 at 10:17
-
@Inquisitive try posting your comment as a question instead and you will get an answer – Raghunandan May 26 '15 at 15:30
-
@Raghunandan: Have a look here http://stackoverflow.com/questions/30473386/regular-expression-validating-pan-card, thanks – inquisitive May 27 '15 at 04:47
-
@Raghunandan: Thanks. – Prashant G Patil Feb 02 '16 at 09:51
-
The regex can be simplified to `[A-Z]{5}[0-9]{4}[A-Z]` (`{1}` is redundant). – Sumit Aug 19 '16 at 14:20
-
Is there an authentic govt/income tax website that gives out the PAN validation algorithm ? – Whirl Mind May 09 '22 at 09:21
-
@WhirlMind not that i am aware of. – Raghunandan May 10 '22 at 03:37
@Raghunandan is right. You can use regex. If you see wiki entry for Permanent_account_number(India) you'll get the meaning of the PAN card number formation. You can use the pattern to check for its validity. Relevant portion is as follows:
PAN structure is as follows: AAAAA9999A: First five characters are letters, next 4 numerals, last character letter.
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.
`
Hope this helps.

- 25,769
- 11
- 95
- 124
-
Thanks @Raghunandan and sunil. This pattern can be used for a more thorougher check on PAN card validity. Sunil, if in Raghunandan's answer, you manage to apply few other restrictions on the fourth character in his answer, you will get what you wish precisely. – Shobhit Puri Jul 16 '13 at 19:05
-
1Does anyone know what algorithm is used for the checksum (last alphabetic check digit)? – Nishan Apr 27 '18 at 14:40
-
1
-
@Nishan Can you provide a resource or more information for the checksum validation? How is Luhn's algorithm used with the letters? – codesniffer Feb 12 '20 at 23:55
-
This is perfect PAN number RegEx: :
String panNumber = "AAAPL1234C"; // get your editext value here
Pattern pattern = Pattern.compile("[A-Z]{3}[ABCFGHLJPTF]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}");
Matcher matcher = pattern.matcher(panNumber );
// Check if pattern matches
if (matcher.matches()) {
Log.i("Matching","Yes");
}
There are some condition for PAN number as follow :
The PAN (or PAN number) is a ten-character long alpha-numeric unique identifier.
The PAN structure is as follows: AAAPL1234C:
The first five characters are letters (in uppercase by default), followed by four numerals, and the last (tenth) character is a letter. The first three characters of the code are three letters forming a sequence of alphabets letters from AAA to ZZZ
The fourth character identifies the type of holder of the card. Each holder type is uniquely defined by a letter from the list below:
- A — Association of persons (AOP)
- B — Body of individuals (BOI)
- C — Company
- F — Firm
- G — Government
- H — HUF (Hindu undivided family)
- L — Local authority
- J — Artificial juridical person
- P — Individual (proprietor)
- T — Trust (AOP)
- F – LLP (limited liability partnership)
The fifth character of the PAN is the first character of either:
- of the surname or last name of the person, in the case of a "personal" PAN card, where the fourth character is "P" or
- of the name of the entity, trust, society, or organisation in the case of a company/HUF/firm/AOP/trust/BOI/local authority/artificial judicial person/government, where the fourth character is "C", "H", "F", "A", "T", "B", "L", "J", "G". The last (tenth) character is an alphabetic digit used as a check-sum to verify the

- 3,842
- 2
- 30
- 50
For more information visit this repository https://github.com/riyastir/PAN-Validator
const validateAlpha = (val) => {
return val.match(/^[A-Za-z]+$/) ? true : false;
};
const validateNum = (val) => {
return val.match(/^\d+$/) ? true : false;
};
const pantype = (val) => {
switch (val) {
case "A":
type = "Association of persons (AOP)";
code = "A";
break;
case "B":
type = "Body of individuals (BOI)";
code = "B";
break;
case "C":
type = "Company";
code = "C";
break;
case "F":
type = "Firm";
code = "F";
break;
case "G":
type = "Government";
code = "G";
break;
case "H":
type = "HUF [Hindu joint family|Hindu undivided family]";
code = "H";
break;
case "L":
type = "Local authority";
code = "L";
break;
case "J":
type = "";
code = "J";
break;
case "P":
type = "Personal";
code = "P";
break;
case "T":
type = "Trust (AOP)";
code = "T";
break;
default:
type = null;
code = null;
return [type, code, false];
}
return [type, code, true];
};
const pan = (panNumber) => {
const firstSet = panNumber.substring(0, 3);
const valFirst = validateAlpha(firstSet);
if (valFirst == true) {
const secondSet = panNumber.substring(3, 4);
const valSecond = pantype(secondSet);
if (valSecond[2] == true) {
const thirdSet = panNumber.substring(5, 9);
const valThird = validateNum(thirdSet);
if (valThird == true) {
const fourthSet = panNumber.substring(9, 10);
const valFourth = validateAlpha(fourthSet);
if (valFourth == true) {
return [valSecond[0], valSecond[1], true];
} else {
return [null, null, false];
}
} else {
return [null, null, false];
}
} else {
return [null, null, false];
}
} else {
return [null, null, false];
}
};
console.log(pan("ABCPA1234D"));

- 21
- 1
- 4
Pan card validation regex:
/(^([a-zA-Z]{5})([0-9]{4})([a-zA-Z]{1})$)/

- 17,741
- 7
- 42
- 75

- 9
- 1
You can use Key press Event for PAN Card Validation in C#
enter code here
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int sLength = textBox1.SelectionStart;
switch (sLength)
{
case 0:
case 1:
case 2:
case 3:
case 4:
if (char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
break;
case 5:
case 6:
case 7:
case 8:
if (char.IsNumber(e.KeyChar) || Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
break;
case 9:
if (char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
if (Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
break;
default:
if (Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
break;
}
}

- 614
- 7
- 4
Note that none of other answers available so far doesn't verify PAN check digit.
Here is Luhn algorythm from http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Java:
public static boolean luhnTest(String number) {
int s1 = 0, s2 = 0;
String reverse = new StringBuffer(number).reverse().toString();
for (int i = 0 ; i < reverse.length(); i++){
int digit = Character.digit(reverse.charAt(i), 10);
if (i % 2 == 0) { //this is for odd digits, they are 1-indexed in the algorithm
s1 += digit;
} else { //add 2 * digit for 0-4, add 2 * digit - 9 for 5-9
s2 += 2 * digit;
if (digit >= 5) {
s2 -= 9;
}
}
}
return (s1 + s2) % 10 == 0;
}

- 24,954
- 11
- 143
- 151
-
1Down vote because PAN number consists of letter and digits while Luhn algorithm is defined only for digits... – bbonev Jun 29 '17 at 18:32
-
@bbonev, it appears I didn't check the question carefully after the title. This answer is about validating [payment card PAN](https://en.wikipedia.org/wiki/Payment_card_number) in java. Unfortunately there is no currently suitable question on SO like for [javascript](https://stackoverflow.com/questions/5869584/javascript-card-pan-check-digit-luhn-verification). Probably it would be better to create it later. – Vadzim Jun 29 '17 at 19:15
Validating proper format should be done by this regex:
/^[A-Z]{3}[ABCFGHLJPT][A-Z][0-9]{4}[A-Z]$/
The difference from other answers is that this one takes into account that fourth letter can only take certain values. The whole regex can easily be changed to be case insensitive.
On the other hand this check is too generic and a proper validation formula for the last check letter would be much better than only checking which position has a digit or letter. Alas this formula seems not to be public.

- 1,406
- 2
- 16
- 33
Hi for pan card verification you need to execute two step process in your project.
1.) create a form and get user pan card number
2.) upload user pan card image
After complete both step make a logic in backend for verification. For this you can follow below tutorial:

- 62
- 3
As per Income Tax Act, the guidelines for PAN Card are as follows:
Format For Pan Card : Eg. ABCDE0123F
Income Tax PAN card is issued under Section 139A of the Income Tax Act. The PAN structure is as follows: AAAPL1234C: The five (5) first characters are letters, followed by four (4) numerals, and the last (10th) character is a letter. The fourth (4th) character informs about the holder of the card.
For more Information regarding Pan card : https://en.m.wikipedia.org/wiki/Permanent_account_number
For Code Validation : "[A-Z]{5}[0-9]{4}[A-Z]{1}"
For Java :
public static boolean isPanCardValid(String pan_number) {
Pattern pattern = Pattern.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}");
Matcher matcher = pattern.matcher(pan_number);
// Check if pattern matches
if (matcher.matches()) {
return true;
} else {
return false;
}
}
where isPanCardValid() is static method, which accepts string as parameter(String pan_number) input from User and matches with PanCard Pattern and returns the value as true or false.

- 241
- 4
- 3
Solution in Java
import java.util.Scanner;
public class PanCard
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the PAN no: "); // char [] pan=sc.nextLine().toCharArray();
String str = sc.nextLine();
if(str.matches ("[A-Z]{5}[0-9]{4}[A-Z]{1}"))
{
System.out.println("Valid PAN no");
}
else
{
System.out.println("Invalid PAN no");
}
}
}
Output
C:\Users\Dell\Desktop\Java Programs> java PanCard
Enter the PAN no:
ASDFG7896K
Valid PAN no

- 51
- 4
Very simple using simple concept.
long l = System.currentTimeMillis();
String s = l + "";
String s2 = "";
System.out.println(s.length());
for (int i = s.length() - 1; i > 8; i--) {
s2+=s.charAt(i);
}
String pancardNo = "AVIPJ" + s2 + "K";
System.out.println(pancardNo);
Use this unique pancard no for testing purpose .

- 1
- 1
^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/
Try This, hope will work

- 2,475
- 10
- 28
- 35

- 1
- 2
Try this one
$(document).ready(function() {
$.validator.addMethod("pan", function(value1, element1) {
var pan_value = value1.toUpperCase();
var reg = /^[a-zA-Z]{3}[PCHFATBLJG]{1}[a-zA-Z]{1}[0-9]{4}[a-zA-Z]{1}$/;
var pan = {
C: "Company",
P: "Personal",
H: "Hindu Undivided Family (HUF)",
F: "Firm",
A: "Association of Persons (AOP)",
T: "AOP (Trust)",
B: "Body of Individuals (BOI)",
L: "Local Authority",
J: "Artificial Juridical Person",
G: "Govt"
};
pan = pan[pan_value[3]];
if (this.optional(element1)) {
return true;
}
if (pan_value.match(reg)) {
return true;
} else {
return false;
}
}, "Please specify a valid PAN Number");
$('#myform').validate({ // initialize the plugin
rules: {
pan: {
required: true,
pan: true
}
},
submitHandler: function(form) {
alert('valid form submitted');
return false;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>
<form id="myform" action="" method="post">
<div>
<label>Pan Number</label>
<div>
<input type="text" name="pan" value="" id="input-pan" />
</div>
</div>
<button type="submit">Register</button>
</form>

- 2,756
- 4
- 33
- 57
public static boolean isPanCardValid(String pan_number) {
Pattern pattern = Pattern.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}");
Matcher matcher = pattern.matcher(pan_number);
// Check if pattern matches
if (matcher.matches()) {
return true;
} else {
return false;
}
}

- 7,311
- 3
- 31
- 36