Articles

Javascript check digit generator

October 4, 2006 at 13:27:53
Specs: Windows XP SP2, P4 3.4Ghz 1GB Ram

I know how to calculate a mod10 check digit manually but does anyone have any javascript that I can plug into an html page? I have searched all over and I only find mod10 validators but not any check digit generators. Thanks

Thanks,
REMGU


See More: Javascript check digit generator

Report •


#1
October 4, 2006 at 14:31:50

Well, I did a few searches and found some VBScript code. I rewrote it in javascript but then found that the "rules" for calculating the mod 10 checksum are apparently different depending on what you are validating. If you were to give me the process you follow I could provide you with the code.

Is the process similar to what is shown on this site: http://www.beachnet.com/~hstiles/ca...

Michael J


Report •

#2
October 4, 2006 at 15:26:53

OK, I think this should do it:

function mod10CheckDigit(barCode) {
//Strip all characters except numbers
bc = barCode.replace(/[^0-9]+/g,'');
total = 0;

//Get Odd Numbers
for (i=0; i<bc.length; i=i+2) {
total = total + parseInt(bc.substr(i,1)) * 3;
}

//Get Even Numbers
for (i=1; i<bc.length; i=i+2) {
total = total + parseInt(bc.substr(i,1));
}

//Determine the checksum
modDigit = (10 - total % 10) % 10;

alert (modDigit);
return modDigit;
}
Michael J


Report •

#3
October 4, 2006 at 16:04:36

Whoops! I had that wrong. I was processing the numbers from left to right and they are supposed to be done right to left. I still see a discrepency on whether the multiplier should be 2 or 3 - I used 2 in the corrected code below because of what I found on this page: http://www.beachnet.com/~hstiles/ca...

Try this out and let me know if it does what you expect:

function mod10CheckDigit(barCode) {
//Strip all characters except numbers
bc = barCode.replace(/[^0-9]+/g,'');
total = 0;

//Get Odd Numbers
for (i=bc.length-1; i>=0; i=i-2) {
total = total + parseInt(bc.substr(i,1));
}

//Get Even Numbers
for (i=bc.length-2; i>=0; i=i-2) {
temp = parseInt(bc.substr(i,1)) * 2;
if (temp > 9) {
tens = Math.floor(temp/10);
ones = temp - (tens*10);
temp = tens + ones;
}
total = total + temp;
}

//Determine the checksum
modDigit = (10 - total % 10) % 10;

alert (modDigit);
return modDigit;
}

Michael J


Report •

Related Solutions

#4
October 16, 2006 at 06:39:20

Thanks Michael, sorry for the late response. I was on vacation. I'll give it a try and let you know.

Thanks,
REMGU


Report •

#5
October 16, 2006 at 07:32:55

Michael,

I am trying to call the function but with no luck. I'm a newbie when using javascript in html. Could you look at the code below and advise? Thanks in advance.

<html>
<head>
<title>Check Digiit Calculation</title>

</body>
</html>

Thanks,
REMGU


Report •

#6
October 16, 2006 at 07:39:24

Sorry, not everything post. Here we go...

<html>
<head>
<title>Check Digiit Calculation</title>

<script type="text/javascript">

function mod10CheckDigit(barCode) {
//Strip all characters except numbers
bc = barCode.replace(/[^0-9]+/g,'');
total = 0;

//Get Odd Numbers
for (i=bc.length-1; i>=0; i=i-2) {
total = total + parseInt(bc.substr(i,1));
}

//Get Even Numbers
for (i=bc.length-2; i>=0; i=i-2) {
temp = parseInt(bc.substr(i,1)) * 2;
if (temp > 9) {
tens = Math.floor(temp/10);
ones = temp - (tens*10);
temp = tens + ones;
}
total = total + temp;
}

//Determine the checksum
modDigit = (10 - total % 10) % 10;

alert (modDigit);
return modDigit;
}

</script>

</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<h1 align="center">Check Digit Calculation</h1>
<form name="barCodeForm" onSubmit="javascript:return mod10CheckDigit">
<table width="400" border="0" cellspacing="0" cellpadding="5" align="center">
<tr>
<td width="150" align="right">Number: </td>
<td width="150">
<input name="Number" type="text" value="123123123" size="18" maxlength="9" id="bc">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Button" value="Get Check Digit" >
</td>
</tr>
</table>
</form>
</body>
</html>


Thanks,
REMGU


Report •

#7
October 16, 2006 at 12:49:26

There are some different problems here.

1. In the onSubmit trigger you are not specifying the function correctly. It needs to be "mod10CheckDigit()" witht he parens.

2. You are not passing the value to the function. So there is no barcode value to process. You need to either pass the value within the parens of the trigger above or you can grap the value from within the function.

3. The onSubmit trigger is written to expect either a true or false to be returned from the function. You have it so the modDigit is returned. This is used for form validations to return false if the user did not enter a required field or something af that nature and it prevents the form from actually being submitted.

I have corrected the code and added some additional functionality:

<html>
<head>
<title>Check Digiit Calculation</title>

<script type="text/javascript">

function mod10CheckDigit() {

//Get barCode field value, Strip all characters
//except numbers Repopulate field w/ new value
bc = document.barCodeForm.barCode.value
bc = bc.replace(/[^0-9]+/g,'');
document.barCodeForm.barCode.value = bc;
total = 0;

//Get Odd Numbers
for (i=bc.length-1; i>=0; i=i-2) {
total = total + parseInt(bc.substr(i,1));
}

//Get Even Numbers
for (i=bc.length-2; i>=0; i=i-2) {
temp = parseInt(bc.substr(i,1)) * 2;
if (temp > 9) {
tens = Math.floor(temp/10);
ones = temp - (tens*10);
temp = tens + ones;
}
total = total + temp;
}

//Determine the checksum
modDigit = (10 - total % 10) % 10;

//Populate the modDigit field with the value
document.barCodeForm.modDigit.value = modDigit;
return false;
}

</script>

</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<h1 align="center">Check Digit Calculation</h1>
<form name="barCodeForm" onSubmit="javascript:return mod10CheckDigit();">
<table width="400" border="0" cellspacing="0" cellpadding="5" align="center">
<tr>
<td width="150" align="right">Number: </td>
<td width="150">
<input name="barCode" type="text" value="123123123" size="18" maxlength="9" id="bc" onKeyDown="this.form.modDigit.value='';">
</td>
</tr>
<tr>
<td width="150" align="right">Mod Digit: </td>
<td width="150">
<input name="modDigit" type="text" value="" size="18" maxlength="9" id="bc" readonly>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Button" value="Get Check Digit" >
</td>
</tr>
</table>
</form>
</body>
</html>

Michael J


Report •

#8
October 16, 2006 at 13:30:55

Thanks Michael. I compared the results from your function to what I get with a known check digit generator application and they do not match so it seems like your algorithm is missing something. Perhaps it would help if I provide the known-to-work manual method of generating the check digit.

To calculate the check digit:

1. Reverse the number

2. Multiply all the digits in odd positions (The first digit, the third digit, etc) by 2.

3. If any one is greater than 9 subtract 9 from it.

4. Sum all numbers up (the result of steps 2 and 3 plus the even numbers)

5. The check digit is the amount you need to add to that number to make a multiple of 10. So if you got 68 in the previous step the check digit would be 2. You can calculate the digit in code using checkdigit = ((sum / 10 + 1) * 10 - sum) % 10

Using the method above...

123456789 would give 7
112233445566 would give 5

Thanks,
REMGU


Report •

#9
October 16, 2006 at 15:15:11

As I stated before I found a lot of different sites with different calculations for the mod digit. I picked the method that seemed most common.

Here is the modified function for your method

function mod10CheckDigit() {

//Get barCode field value, Strip all characters
//except numbers Repopulate field w/ new value
bc = document.barCodeForm.barCode.value
bc = bc.replace(/[^0-9]+/g,'');
document.barCodeForm.barCode.value = bc;
total = 0;

//Get Odd Numbers
for (i=bc.length-1; i>=0; i=i-2) {
temp = parseInt(bc.substr(i,1))*2;
total += (temp > 9)?temp-9:temp;
}

//Get Even Numbers
for (i=bc.length-2; i>=0; i=i-2) {
total = total + parseInt(bc.substr(i,1));
}

//Determine the checksum
modDigit = (10 - total % 10) % 10;

//Populate the modDigit field with the value
document.barCodeForm.modDigit.value = modDigit;
return false;

}

Michael J


Report •

#10
October 16, 2006 at 18:06:45

Mike, that worked!! I can't thank you enough for your time, it's poeple like you that make this an awesome site.

Thanks again man!

REMGU

Thanks,
REMGU


Report •

#11
October 16, 2006 at 18:49:41

No problem. Would you like my paypal account number now? JK, it was my pleasure.

Michael J


Report •


Ask Question