Background
In many forum posts I have read one common question. That is, how to validate a Permanent Account Number (PAN) card. But no one provides the proper solution so by considering that requirement I have decided to write this article. So let us learn about this requirement step-by-step.
PAN Cards
A PAN is an identifier of Indian Tax Payers and is a 10 alphanumeric unique number.
A PAN card has the following characteristics.
- Is a combination of letters and numbers having a length of 10
- All letters are in upper case.
- A PAN card number always starts and ends with letters.
- Does not contain special characters.
- The PAN card number starts with five letters then has four digits and the last one is a letter.
- Each holder is uniquely defined as 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 Judicial Person
- P — Individual
- T — AOP (Trust)
- B — Body of Individuals (BOI)
- A — Association of Persons (AOP)
Suppose the PAN card for a person starts with an A, for example:
AAAPL1234C
Step 1: Now from the preceding PAN card structure, let us create the regular expression for it as:
- [A-Z]{5}\d{4}[A-Z]{1}
Step 2: Create the web application as in the following.
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
- Provide the web site a name such as "PANcardValidation" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" then add a Web Form.
- Drag and drop one TextBox onto the <form> section of the Default.aspx page.
- Now the Default.aspx page source code will look such as follows.
Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body style="background-color: #000080">
- <form id="form1" runat="server">
- <h5 style="color: #FF3300; font-size: large;">
- PAN Card Validation</h5>
- <table style="margin-top: 20px; color: White">
- <tr>
- <td>
- Enter PAN Card
- </td>
- <td>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- </td>
- <td>
- <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
- Display="Dynamic" ForeColor="Red" ErrorMessage="InValid PAN Card" ValidationExpression="[A-Z]{5}\d{4}[A-Z]{1}"></asp:RegularExpressionValidator>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Step 3: Now run the application, the UI will be look as follows:

Now enter an invalid PAN card number with a small letter; it wil show the following error message:

In the preceding it shows an error because all the letters are in small case and not in the proper structure.
Now enter following input and again it shows the error message as:
In the preceding image, it shows the error message even 10 characters in length and all capital letters because the PAN card structure does not match.
Now enter the following input with one letter missing, again it shows the following error:
In the preceding image, it shows the error message even all capital letters because one letter is missing.
Now enter a proper PAN number, it will not show an error as:

In the preceding example, it does not show an error message because the preceding PAN number is correct as per the structure.
Note
- For the detailed code, please download the sample Zip file
- I have used a dummy PAN card, you can use any PAN number
Summary
For all the preceding examples, we have learned how to validate the PAN card. I hope this article is useful for all readers. If you have a suggestion then please contact me.

Vithal WadjePosted Jan 13, 2015, 1:07 PM
which error
santosh kumarPosted Jan 13, 2015, 8:11 AM
throwing code issues
Vithal WadjePosted Nov 7, 2014, 7:12 AM
Download file
santosh kumarPosted Nov 6, 2014, 12:51 PM
not working perfectly
Jaganathan BantheswaranPosted Nov 6, 2014, 4:00 AM
You are welcome Mr. Vithal.
Vithal WadjePosted Nov 6, 2014, 1:57 AM
Jaganathan Bantheswaran sir,i have noted your points,thanks for your suggestion
Jaganathan BantheswaranPosted Nov 6, 2014, 12:46 AM
Vithal, You understood me wrong. I never say it is not-technical but i meant to say its a kind of validation using regular expression and there is nothing much about asp.net so it could be posted under codesnippet thats it. it is my suggestion, so it is your call to take it up or not. If you dont feel something like that you could have simply ignored it rather than fighting like this. Why are you taking it wrong. Yes, even i dont know how the PAN number is validated, so its useful for me too. but my concern is that the title could be something different and posted as codesnippet & even PAN number details could beposted as separate article itself. thats it.
Vithal WadjePosted Nov 5, 2014, 11:16 PM
wait i will delete article soon,i will request to C# corner team if it is not technical then delete this,if they delete then consider its non technical,if not then you self consider this article helping other people instead of you.Many articles i have written on demand of people asking me solution on it same thing asked me by someone so i have did it,if post just code snippet without explaining what is the structre of PAN then no developer in this world can understand how to do validation for PAN card,also US,UK other people will not be undestand about that codesnippet because they dont know PAN card and numbering strucuture. search on google how many peoples wants solution for this ,you find many peoples asking same qeustions.i wants to just give you message that without undestanding proper reason dont judge anyone contribution, i was wasted whole day to prepare this solution with negative validation. and C# corner is like family member for me how to care about it i wel known about it
Jaganathan BantheswaranPosted Nov 5, 2014, 10:37 PM
Cool mr.vithal, I am c# corner user who has rights to post comments on all the articles. If i find some duplicate questions on forum, i used to reply that dont post duplicate questions. It is not discouraging user but making them to use c#corner in right way. I felt something like that and commented here. Taking comments in right way is upto you, if you want only positive comments then mention that i need only positive comments as in your articles. You dont have right to stop me to post comments on article which i read in c#corner.
Jaganathan BantheswaranPosted Nov 5, 2014, 8:00 AM
I am not here to discourage anyone here. What is there in my comment to discourage you??. I just meant that this article could be posted under CodeSnippet. what is there to discourage you?. Why should i discourage you?. Even i could see couple of comments also mentioning about the PAN info only not about the technical as per the title. And YES, i [will] do post comments on articles if i find something to improve on that article NOT to discourage.
Jaganathan BantheswaranPosted Nov 5, 2014, 6:45 AM
I just suggested that it could be posted under codesnippet because this article has nothing much to explain technically about the ASP.Net code I FEEL. It explains just about the PAN card validation, so i thought it could be a simple codesnippet, not an article to explain much. If you feel bad about my suggestion, just ignore it.
Vithal WadjePosted Nov 4, 2014, 12:40 AM
thanks Saineshwar Bageri sir
Vithal WadjePosted Nov 4, 2014, 12:40 AM
thanks Udaya kumar sir
Saineshwar BageriPosted Nov 4, 2014, 12:09 AM
Nice one sir
Udaya kumarPosted Nov 3, 2014, 11:49 PM
Good information
Vithal WadjePosted Nov 3, 2014, 4:19 AM
Thanks Manju Madam
Manju lata YadavPosted Nov 3, 2014, 12:56 AM
thanks for providing that much information abt pan card.