Wednesday, 18 January 2017

Hi friends...

This is my first post..I just wanted to explain here regarding aadhar card number validation using verhoeff algorithm.. Firstly create the webform(saying default.aspx) here is my code.Below you can see some lines which are commentable. The thing is that i just tried it to validate aadhar number by using javascript but finally i got a failure result.Then i tried it using a class in which i got a success to do it.So,In webform,i am using one textbox,one label and one button.


Default.aspx (only numeric values allowed )


<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblmsg" runat="server"></asp:Label>
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <asp:Button ID="btnbutton" runat="server" OnClick="btnbutton_Click" Text="Validate" />
    </div>
    </form>
</body>


Here i used a button to call a class... Code Behind:



Namespace 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

Button Onclick Function

protected void btnbutton_Click(object sender, EventArgs e)
    {

        ScriptManager.RegisterClientScriptBlock(this, GetType(), "none", "", false);


        ClientScript.RegisterStartupScript(this.GetType(), "Popup", "validate();", true);
        aadharcard aad = new aadharcard(); 
        bool isValidnumber = aadharcard.validateVerhoeff(TextBox1.Text);
        //aadharcard.validateVerhoeff("num");
        lblmsg.Text = TextBox1.Text + "valid number";
        if (isValidnumber)
        {
            //lblmsg.ForeColor = Color.Red;
            lblmsg.Text = "valid number";
        }
        else
        {
            //lblmsg.ForeColor = Color.Green;
            lblmsg.Text = "Invalid number";
        }
    }

 Below is my class: aadharcard.cs

public class aadharcard
    {
        static int[,] d = new int[,]
    
    { 
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 
    {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
    {2, 3, 4, 0, 1, 7, 8, 9, 5, 6}, 
    {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
    {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
    {5, 9, 8, 7, 6, 0, 4, 3, 2, 1}, 
    {6, 5, 9, 8, 7, 1, 0, 4, 3, 2}, 
    {7, 6, 5, 9, 8, 2, 1, 0, 4, 3}, 
    {8, 7, 6, 5, 9, 3, 2, 1, 0, 4}, 
    {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} 
    };
        static int[,] p = new int[,] 
         {
         {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
         {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
         {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
         {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
         {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
         {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, 
         {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, 
         {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}
         };

        static int[] inv = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };

        public static bool validateVerhoeff(string num)
        {
            int c = 0; int[] myArray = StringToReversedIntArray(num);
            for (int i = 0; i < myArray.Length; i++)
            {
                c = d[c, p[(i % 8), myArray[i]]];
            } return c == 0;

        }
        public static bool validateAadharNumber(String aadharNumber)
        {
            //Pattern aadharPattern = Pattern.compile("\\d{12}");

            //bool isValidAadhar = aadharPattern.matcher(aadharNumber).matches();

            //if (isValidAadhar)
            //{

            //    isValidAadhar = aadharcard.validateVerhoeff(aadharNumber);

            //}

            //return isValidAadhar;
            return true;
        }


        public static string generateVerhoeff(string num)
        {
            int c = 0; int[] myArray = StringToReversedIntArray(num);
            for (int i = 0; i < myArray.Length; i++)
            {
                c = d[c, p[((i + 1) % 8), myArray[i]]];
            }
            return inv[c].ToString();
        }

        private static int[] StringToReversedIntArray(string num)
        {
            int[] myArray = new int[num.Length];
            for (int i = 0; i < num.Length; i++)
            {
                myArray[i] = int.Parse(num.Substring(i, 1));
            }
            Array.Reverse(myArray); return myArray;
        }
    }

Thank You,

Please Like and Share My Post.....

10 comments: