2016-08-29 4 views
0

下記のjavascriptコードは、入力されたaadhar番号が有効かどうかを検証するためのものです。私がしたいのは、ユーザーがaadhar番号を入力するときに、入力された番号が有効かどうかを検証する必要があります。妥当性は検証されていません。入力されたaadhar番号がverhoeffアルゴリズムを使用して有効かどうかを検証する方法は?

<script language="javascript" type="text/javascript"> 

    var d = [ 
     [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] 
    ]; 

    $('#UserForm').formValidation({ 
         message: 'This value is not valid', 
         feedbackIcons: { 
          valid: 'glyphicon glyphicon-ok', 
          invalid: 'glyphicon glyphicon-remove', 
          validating: 'glyphicon glyphicon-refresh' 
         }, 
         fields: {aadhaar_no: { 
           validators: { 
            digits: { 
             message: 'Please use numeric characters only.' 
            }, 
            stringLength: { 
             min: 12, 
             max: 12, 
             message: 'The aadhaar number must be 12 characters long' 
            }, identical: { 
             field: 'c_aadhaar_number', 
             message: 'The aadhaar number and its confirm field are not the same' 
            }, callback: { 
             message: 'The input string is not a valid Aadhaar number.', 
             callback: function (value, validator, $field) { 
              return validate(value); 


         } 
+0

コードの最後が欠落しているようです。なぜ "c#"タグ? – nnnnnn

+4

質問は何ですか? – Guy

+0

質問はaadhar番号の検証についてです。ユーザーがaadhar番号を入力すると、入力された番号が有効かどうかが表示されます。 –

答えて

0

事はでは、私はちょうどJavaScriptを使用してaadhar番号を検証することを試みたが、私はit.Soを行うための成功を得ているクラスを使用して、それを試してみましたresult.Then最終的に私は失敗を得たことです私は1つのテキストボックス、1つのラベルと1つのボタンを使用しています。

Default.aspxを(ソースコード):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <%-- <script type="text/javascript" src="Javascript/jquery.js"></script> 

<script language="javascript" type="text/javascript"> 

    var d = [ 
    [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] 
]; 

// permutation table p 
var p = [ 
    [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] 
]; 

// inverse table inv 
var inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]; 

// converts string or number to an array and inverts it 
function invArray(array) { 

    if (Object.prototype.toString.call(array) === "[object Number]") { 
     array = String(array); 
    } 

    if (Object.prototype.toString.call(array) === "[object String]") { 
     array = array.split("").map(Number); 
    } 

    return array.reverse(); 

} 

// generates checksum 
function generate(array) { 

    var c = 0; 
    var invertedArray = invArray(array); 

    for (var i = 0; i < invertedArray.length; i++) { 
     c = d[c][p[((i + 1) % 8)][invertedArray[i]]]; 
    } 

    return inv[c]; 
} 

// validates checksum 
function validate(array) { 

    var c = 0; 
    var invertedArray = invArray(array); 

    for (var i = 0; i < invertedArray.length; i++) { 
     c = d[c][p[(i % 8)][invertedArray[i]]]; 
    } 

    return (c === 0); 
} 

$(document).ready(function() { 
    $('#UserForm').formValidation({ 
        message: 'This value is not valid', 
        feedbackIcons: { 
         valid: 'glyphicon glyphicon-ok', 
         invalid: 'glyphicon glyphicon-remove', 
         validating: 'glyphicon glyphicon-refresh' 
        }, 
        fields: {aadhaar_no: { 
          validators: { 
           digits: { 
            message: 'Please use numeric characters only.' 
           }, 
           stringLength: { 
            min: 12, 
            max: 12, 
            message: 'The aadhaar number must be 12 characters long' 
           }, identical: { 
            field: 'c_aadhaar_number', 
            message: 'The aadhaar number and its confirm field are not the same' 
           }, callback: { 
            message: 'The input string is not a valid Aadhaar number.', 
            callback: function (value, validator, $field) { 
             return validate(value); 
            } 
           } 
          } 
         } 
}); 
      }); 

    </script>--%> 
</head> 
<body> 
    <form id="UserForm" runat="server"> 
    <div> 

     <asp:Label ID="Label1" runat="server" Text="Aadhar Number: "></asp:Label> 
&nbsp;<asp:TextBox ID="TextBox1" runat="server" MaxLength="12"></asp:TextBox> 
     &nbsp;<asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
      runat="server" ControlToValidate="TextBox1" 
      ErrorMessage="only numeric values allowed" ForeColor="Red" 
      ValidationExpression="\d+">only numeric values allowed</asp:RegularExpressionValidator> 
     <br /> 
     <br /> 
&nbsp;&nbsp;&nbsp;<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 


     <asp:Button ID="aadhar_number" runat="server" onclick="Button1_Click" 
      onclientclick="generate()" Text="check" /> 


    </div> 
    </form> 
</body> 
</html> 

は、私は後ろにクラス コードを呼び出すためのボタンを使用:

以下
protected void Button1_Click(object sender, EventArgs e) 
    { 
     //ScriptManager.RegisterClientScriptBlock(this, GetType(), "none", "<script>executeAfter();</script>", false); 
     // ClientScript.RegisterStartupScript(this.GetType(), "Popup", "validate();", true); 

     aadharcard aad = new aadharcard(); 
     bool isValidnumber = aadharcard.validateVerhoeff(TextBox1.Text); 
     // aad.validateVerhoeff(""); 

     //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"; 
     } 

    } 

は私のクラスである: aadharcard.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Text.RegularExpressions; 

/// <summary> 
/// Summary description for aadharcard 
/// </summary> 
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} 
     }; 

    // The permutation table 
    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} 
     }; 

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


    /// <summary> 
    /// Validates that an entered number is Verhoeff compliant. 
    /// NB: Make sure the check digit is the last one! 
    /// </summary> 
    /// <param name="num"></param> 
    /// <returns>True if Verhoeff compliant, otherwise false</returns> 
    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; 
    //} 

    /// <summary> 
    /// For a given number generates a Verhoeff digit 
    /// Append this check digit to num 
    /// </summary> 
    /// <param name="num"></param> 
    /// <returns>Verhoeff check digit as string</returns> 
    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(); 
    } 


    /// <summary> 
    /// Converts a string to a reversed integer array. 
    /// </summary> 
    /// <param name="num"></param> 
    /// <returns>Reversed integer array</returns> 
    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; 

    } 
} 

だから、実行するだけで必要な結果が得られます。 歓声.. :)

関連する問題