try
{
string txtId = p_text.PadLeft(9, '0');
int sum = 0;
int[] arrIdDigits = new int[9];
for (int i = 0; i < arrIdDigits.Length; i++)
{
arrIdDigits[i] = int.Parse(p_text[i].ToString());
if (i % 2 != 0)
{
arrIdDigits[i] *= 2;
if (arrIdDigits[i] > 9)
{
arrIdDigits[i] = (arrIdDigits[i] % 10) + 1;
}
}
sum += arrIdDigits[i];
}
sum = (sum % 10);
if (sum == 0)
{
return true;
}
}
catch (Exception ex)
{
}
return false;
=======================
ReplyDeleteSOURCE CODE AS HTML:
=======================
<html>
<head>
<title>ID TEST</title>
<script language="javascript" type="text/javascript">
function pad(number, length, p_paddingChar) {
var str = '' + number;
while (str.length < length) {
str = p_paddingChar + str;
}
return str;
}
function IsValidID(p_text) {
try {
var txtId = pad(p_text, 9, '0');
var sum = 0;
var digit;
for (var i = 0; i < 9; i++) {
digit = parseInt(txtId[i]);
if (i % 2 != 0) {
digit *= 2;
if (digit > 9) {
digit = (digit % 10) + 1;
}
}
sum += digit;
}
sum = (sum % 10);
if (sum == 0) {
return true;
}
} catch (e) {
// todo:
alert(e.Message + '----' + e.LineText);
}
return false;
}
</script>
</head>
<body>
<input id="inputID" type="text" size="100" />
<button onclick="alert(IsValidID(document.getElementById('inputID').value));">
CHECK ID</button>
</body>
</html>
Here is online working code:
ReplyDeletehttp://jsfiddle.net/QtjFR/3/