satyawan
satyawan

Reputation: 507

pancard structure validation in javascript and php also

i want to do pancard validation in javascript only.

Its limited to india only.

the structure of pancard in India is as follows.. for example : AAAAA9999A

Each deductee is uniquely identified by the PAN If the PAN does not follow the above structure, then the PAN will be shown invalid

The fourth character of the PAN must be one of the following, depending on the type of assessee:

The fifth character of the PAN is the first character in the surname of the person to whom the PAN belongs.

so i want of check first five letters are alphabetic then follow by four letter numeric and last one is alphabetic.

so i create javascript as follow.

var panVal = $('#panNumber').val);
 var regpan = /^([a-zA-Z])([0-9])([a-zA-Z])?$/;
if(regpan.test(panVal)){
   // valid pan card number
}else // invalid pan card number

but its not working for me.

help is more appreciated for me.

Thank in advance.

Upvotes: 10

Views: 54951

Answers (12)

KUNWAR JODHPUR
KUNWAR JODHPUR

Reputation: 1

function IsValidNumaricValue(String){
    let numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
    if(numberRegex.test(String)) {
      return true;
    } else {
      return false;
    }  
}

function IsValidLatter(ch){
    return /^[A-Z]$/i.test(ch);
}

function IsValidPanNumber(panNumber) {
    const regex = /[A-Z]{5}[0-9]{4}[A-Z]{1}/;   
    if (panNumber.length != 10 ) { return false; }  
    if (regex.test(panNumber) == false) { return false }
    const charArray = panNumber.split('');
    var IsValidPan = true;
    charArray.forEach((CharOrNumber, index)=>{
        if(index < 5){
            if(IsValidLatter(CharOrNumber) == false){
                IsValidPan = false;
            }
        } else if (index > 4 && index < 9){
            if(IsValidNumaricValue(CharOrNumber) == false){
                IsValidPan = false;

            }
        } else if(index == 9){
            if(IsValidLatter(CharOrNumber) == false){
                IsValidPan = false;
            }
        }
    });
    return IsValidPan;
}

Upvotes: 0

Adarsh Madrecha
Adarsh Madrecha

Reputation: 7926

const regex = /[A-Z]{3}[PCHFATBLJG]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}$/;
  • 1st to 3rd Char - Capital Alphabets
  • 4th Char - Alphabet from one of these PCHFATBLJG
  • 5th Char - Capital Alphabets
  • 7th to 9th - Numbers
  • 10th Char - Capital Alphabets

4th Character definition

const fullForm = {C:"Company", P:"Personal", H:"Hindu Undivided Family (HUF)", F:"Firm/LLP", A:"Association of Persons (AOP)", T:"AOP (Trust)", B:"Body of Individuals (BOI)", L:"Local Authority", J:"Artificial Juridical Person", G:"Govt"};

Upvotes: 0

Amit Kumar
Amit Kumar

Reputation: 1

<html>
<form name="myForm" action="/action_page.php" onsubmit="return ValidatePANsubmit(pancard)" method="post">
<input type="text" name="name" id="name" MaxLength="50" placeholder="Full Name">
<br><br>
<input type="text" name="pancard" id="pancard" MaxLength="10" placeholder="Pan Number" onblur="ValidatePAN(this);">

 <input type="submit" value="Submit">
 </form>
</html>

<script>
function ValidatePAN(pancard) {  
 

if (pancard.value != "") {
            ObjVal = pancard.value;
            ObjValLength = pancard.value.length;
            ObjVal = ObjVal.toUpperCase();
            
            var panPat = /[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[ObjVal[3]];


            if (ObjValLength != 10) 
            {
            alert("PAN Number should be 10 digit !");
// set the value to null - if input id name = pan & form name = myform
//  document.forms['myform'].pan.value = "";
            return false;
            }
  
            if (ObjVal.search(panPat) == -1) {
                alert("Invalid Pan No.");
                pancard.focus();
                return false;
            }
            if(pan!="undefined"){
                alert(pan+" - Pan No. detected - OK");
                                }
            else
            {
                alert("Unknown Pan card No.");
                return false;
            }
                            }
                        }
</script>
  
<script>
function ValidatePANsubmit(pancard) {  

        if (pancard.value != "") {
            ObjVal = pancard.value;
            ObjValLength = pancard.value.length;
            ObjVal = ObjVal.toUpperCase();
            
            
            var ObjValmatch = ObjVal.charAt(4);
//          alert(ObjValmatch);

            nameval = document.getElementById('name').value;
            if (nameval == "")
            {
            alert("Name not Entered !");
            return false;
            }
            var namevalmatch = nameval.match(/\b\w/g) || [];
            namevalmatch = ((namevalmatch.pop() || '')).toUpperCase();
//          alert(namevalmatch);
            
            var panPat = /[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[ObjVal[3]];


            if (ObjValLength != 10) 
            {
            alert("PAN Number should be 10 digit !");
// set the value to null - if input id name = pan & form name = myform
//  document.forms['myform'].pan.value = "";
            return false;
            }
  
            if (ObjVal.search(panPat) == -1) {
                alert("Invalid Pan No.");
                pancard.focus();
                return false;
            }
            

            if (ObjValmatch != namevalmatch) 
            {
            alert("Pan no. 5th Character -- "+ObjValmatch+" -- not match with - Name Title First Character ! -- "+namevalmatch+" --");
// set the value to null - if input id name = pan & form name = myform
//  document.forms['myform'].pan.value = "";
            return false;
            }
            
            
            
            
            if(pan!="undefined"){
//              alert(pan+" - Pan No. detected - OK");
                                }
            else
            {
                alert("Unknown Pan card No.");
                return false;
            }
                            }
            else
            {
            alert("Pan No. not Entered !");
            return false;                   // if pan no. not entered stop submit
            }
                        }
  </script>

Upvotes: 0

Pawan Rathore
Pawan Rathore

Reputation: 41

Use as provided:

if(!empty($_POST["panNumber"]) && $_POST["panNumber"] != "" ){

    $panNumber = $_POST["panNumber"];
    if (!preg_match('/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/',$_POST["panNumber"])) {
        $err .= "Only 11 digit pin code are allowed which containes 5 Charactor, 4 alphabate & 1 charctor in <strong> PAN NUMBER </strong>"."<br>";
    }else{
        $panNumber = $_POST["panNumber"];
    }       
}else{
    $err .= "Please Enter your PAN CARD number"."<br>";
}

Upvotes: 0

kishan verma
kishan verma

Reputation: 1000

As per Income Tax Act, the guidelines for PAN Card are as follows:

Format For Pan Card : Eg. ABCDE0123F

    var panVal = $('ABCDE0123F').val();

    var vali = /^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/;
if(vali.test(panVal)){

   // valid pan card number

} else {

   // invalid pan card number
}

Upvotes: 0

Use the following code:

function pan_validate(pan) {
  var regpan = /^([A-Z]){5}([0-9]){4}([A-Z]){1}?$/;
  if (regpan.test(pan) == false) {
    document.getElementById("status").innerHTML = "PAN Number Not Valid.";
  } else {
    document.getElementById("status").innerHTML = "";
  }
}
<input type="text" class="form-control input_capital" MaxLength="10" onkeyup="pan_validate(this.value);" placeholder="Please Enter Pan Card" name="pancard" autocomplete="off">
<b style="color:red"><span id="status"></span></b>

Upvotes: 0

sagar varade
sagar varade

Reputation: 61

For JavaScript you can use

function validatePAN(panVal){
    var regpan = /^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/;
    if(regpan.test(panVal)){
        return true;
    } else {
        return false;
    }
}

Upvotes: 1

Arun
Arun

Reputation: 3731

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.

Upvotes: 30

Umesh Patil
Umesh Patil

Reputation: 4968

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!!

Upvotes: 6

Manav Akela
Manav Akela

Reputation: 174

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... :)

Upvotes: 5

RJParikh
RJParikh

Reputation: 4166

Try below code.

function ValidatePAN() { 
  var Obj = document.getElementById("textPanNo");
        if (Obj.value != "") {
            ObjVal = Obj.value;
            var panPat = /^([a-zA-Z]{5})(\d{4})([a-zA-Z]{1})$/;
            if (ObjVal.search(panPat) == -1) {
                alert("Invalid Pan No");
                Obj.focus();
                return false;
            }
          else
            {
              alert("Correct Pan No");
              }
        }
  } 
<input type="text" ID="textPanNo" MaxLength="10" onblur="ValidatePAN(this);">
<input type="button" value="Check" onclick="ValidatePAN();">

Upvotes: 1

Bindesh Pandya
Bindesh Pandya

Reputation: 193

I have my working script here to validate Pan Number, I think it should help you.

    function validatePanNumber(panNum)
         {

           var regpan = /^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/;

           if(regpan.test(panNum) == false)
           {

              document.getElementById("status").innerHTML = "Permanent Account Number is not yet valid.";

           }
           else
            {

            document.getElementById("status").innerHTML = "You have entered a valid Permanent Account Number !";

            }
       }

Now Call this function on key event of text box

  <input type="text" name="panNum" onkeyup="validatePanNumber(this.value);"> 

Upvotes: 0

Related Questions