pancard structure validation in javascript and php also

There is a logical error in your code. Try the below code:

var panVal = $('#panNumber').val();
var regpan = /^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/;

if(regpan.test(panVal)){
   // valid pan card number
} else {
   // invalid pan card number
}

You have to limit the characters for how many time they have to occur in the given string.

Explanation

  1. ([a-zA-Z]){5} -> Alphabets should be 5 in number.

  2. ([0-9]){4} -> Numbers should be 4 in number.

  3. ([a-zA-Z]){1} -> Alphabets should be 1 in number.


Try this code it also tells about card type as asked by @RJParikh

function pan(txt)
{
	txt = txt.toUpperCase();
	var regex = /[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[txt[3]];
	if(regex.test(txt))
	{
    if(pan!="undefined")
      alert(pan+" card detected");
    else
	  alert("Unknown card");
    }
    else
	  alert("Unknown card");
}
<input id="pan">
<button onclick="pan(document.getElementById('pan').value)">search</button>

Hope it works well... :)


Well, with HTML 5 you don't have to write any javascript to add validation, at least for this PAN validation, use below input tag for this,

<input type="text" id="pan_no" name="pan_no" placeholder="PAN No." maxlength="10" pattern="[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}" title="Please enter valid PAN number. E.g. AAAAA9999A" required/>

Enjoy!!

Tags:

Javascript

Php