DotNetSlackers: ASP.NET News for lazy Developers

Thursday, October 15, 2015

Javascript PAN Number validation in Asp.net Textbox onkeypress event

Explanation

Permanent Account Number is simply called as (PAN) in India who are all paying the TAX using this identification card.PAN is ten digit alphanumeric characters.All characters should be in upper case.The first five characters should be Alphabets and followed by four digit numbers and the last one should be Alphabet.The first character is the type of the PAN and the fifth character is the name of the person.

  For example an individual person Bharat having a PAN card and his PAN number is

        CAMCB3456K

First Letter C-Individual
Fifth Letter B-His name starts with Bharat.

You may find N number of approaches for validating the PAN using javascript but in this post i have tried to validate the PAN number in textbox onkeypress event.

Design
 

<table cellpadding="2" cellspacing="3" border="1" align="center" width="420px">            
            <tr>
                <td>
                    Pan Number
                </td>
                <td>
                    <asp:TextBox ID="txtPanNumber" runat="server" Width="268px" onkeypress="return ValidatePAN(this.id);"></asp:TextBox>
                </td>
            </tr> 
        </table>

Javascript
 

<script type="text/javascript">
        /*Pan Card Number First 5 Letter should be Character and following next 4 letter should be numbers and last 1 should be Character and it should not exceed 10*/

        function ValidatePAN(ObjID) {
            var PANNumber = document.getElementById(ObjID).value;
            var count = PANNumber.length;
            var keyCode = (window.event.which) ? parseInt(window.event.which) : parseInt(window.event.keyCode);

            if (count <= 4) {
                if ((keyCode <= 90 && keyCode >= 65)) {
                    return true;
                }
                else {
                    return false;
                }
            }
            if (count <= 8 && count >= 5) {
                if (keyCode <= 57 && keyCode >= 48) {
                    return true;
                }
                else {
                    return false;
                }
            }

            if (count == 9) {
                if ((keyCode <= 90 && keyCode >= 65)) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }

    </script>


Output
 
In this post i tried to validate the PAN number validation using javascript Asp.net textbox onkeypress event.

No comments:

Post a Comment