下面提到的javascript代码用于验证输入的aadhar号码是否有效。我想要做的是当用户输入aadhar号码时,它应该验证输入的号码是否有效。我使用了javascript用于验证但不起作用。
<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 :(得分:0)
问题是我只是尝试通过使用javascript来验证aadhar数字,但最后我得到了失败的结果。然后我尝试使用我成功的类来做它。所以,在webform中,我是使用一个文本框,一个标签和一个按钮。
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>
<asp:TextBox ID="TextBox1" runat="server" MaxLength="12"></asp:TextBox>
<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 />
<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
<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;
}
}
那么,那么你只需运行它就可以获得所需的结果。 欢呼..:)
答案 1 :(得分:0)
使用此算法来验证aadhara编号。对于其他语言,请参考此https://en.wikibooks.org/wiki/Algorithm_Implementation/Checksums/Verhoeff_Algorithm
VM options
答案 2 :(得分:0)
<input type="text" id="test" onchange="validateVerhoeff(this);"/>
<script>
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]
]
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]
]
功能validateVerhoeff(obj)
{
var aadharNumber=obj.value;
if(aadharNumber.length!=12)
{
alert("Aadhar should be 12 Digit");
obj.value="";
obj.focus();
return false;
}
if(isNaN(aadharNumber))
{
alert("Aadhaar must be a number");
obj.value="";
obj.focus();
return false;
}
var c = 0;
var invertedArray = aadharNumber.split('').map(Number).reverse();
var len = invertedArray.length;
for (var i = 0; i < len; i++)
{
c = d[c][p[(i % 8)][invertedArray[i]]];
}
return (c === 0);
}
</script>