Masked PAN Number or Debit/Credit Card Number

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 3532
Sometimes we need not to show full PAN Number or we can say that Bank Account number or Debit/Credit card number,then we can show last 4 deigit of number,and rest number we can MASK.
So that no body will see the whole number.Here,i am taking PAN number example, and Masked first 6 digit as X.
We have to create a property as PAN_Masked,and use any where it is going to be used.
Below is my code:-
public string PAN_Masked
{
get
{
if (!string.IsNullOrEmpty(PAN_Number))
{
StringBuilder sb_masked = new StringBuilder(PAN_Number.Length);
int mask = 6;

for (int i = 0; i < PAN_Number.Length; i++)
{
if (i < mask)
{
sb_masked.Append('X');
}
else
{
sb_masked.Append(PAN_Number[i]);
}
if ((i == 2) || (i == 4) || (i == 5)) sb_masked.Append('-');
}
return sb_masked.ToString();
}
else
return null;
}
}

To use above PAN_Masked,
string PAN_Number = "ABCDE1234F";

Response.Write(PAN_Masked);


Output:-
XXX-XX-X-234F

Comments or Responses

Login to post response