...

View Full Version : EAN/ISBN13 check digit verification



de1
02-10-2009, 05:02 PM
Hi again,
As noted in my last post I've been trying to teach myself javascript over the last few weeks in order to build a script that can perform check digit verification. Also again, my apologies up front for any glaring newbie mistakes. Any help is VERY MUCH appreciated!!!

How I'd like it to work: When a user has entered an EAN number (13 digits, all numbers) in a text field (called "InsertRecordEAN") and leaves this field (onblur, I believe) the script will run, checking it's last digit/check digit against what I've calcucated it should be. If the check digit is incorrect an alert box will pop up saying so, and when they click OK it highlights the incorrect value, not letting them leave that field until it's corrected.

I've learned quite a lot from your posts in the last couple days, and everything I have below SEEMS like it should work, but I get nothing. Any ideas, at all?

Thanks in advance!!!!!!
d



<html>
<head>
<script type="text/javascript">
<!--
-->
</script>
<script type="text/javascript">
<!--
function ErrorAlert()
{
document.getElementByName("InsertRecordEAN").onblur = alert("The EAN you entered is incorrect, please re-enter it.")
document.getElementByName("InsertRecordEAN").focus();
}
-->
</script>

<script type="javascript">
<!--
//**seems to be held up by UserEAN not getting passed to function**
function EANCheck(UserEAN)
{
//2 parse EAN and converted to num except m (var m is check digit supplied by user)
var a = parseInt(UserEAN.charAt(UserEAN.length-13)); //digit1
var b = parseInt(UserEAN.charAt(UserEAN.length-12)); //digit2
var c = parseInt(UserEAN.charAt(UserEAN.length-11)); //digit3
var d = parseInt(UserEAN.charAt(UserEAN.length-10)); //digit4
var e = parseInt(UserEAN.charAt(UserEAN.length-9)); //digit5
var f = parseInt(UserEAN.charAt(UserEAN.length-8)); //digit6
var g = parseInt(UserEAN.charAt(UserEAN.length-7)); //digit7
var h = parseInt(UserEAN.charAt(UserEAN.length-6)); //digit8
var i = parseInt(UserEAN.charAt(UserEAN.length-5)); //digit9
var j = parseInt(UserEAN.charAt(UserEAN.length-4)); //digit10
var k = parseInt(UserEAN.charAt(UserEAN.length-3)); //digit11
var l = parseInt(UserEAN.charAt(UserEAN.length-2)); //digit12
var m = parseInt(UserEAN.charAt(UserEAN.length-1)); //digit13

//3 multiply each digit against weighting factors to get extended value
var a1 =(a * 1);
var b1 =(b * 3);
var c1 =(c * 1);
var d1 =(d * 3);
var e1 =(e * 1);
var f1 =(f * 3);
var g1 =(g * 1);
var h1 =(h * 3);
var i1 =(i * 1);
var j1 =(i * 3);
var k1 =(i * 1);
var l1 =(i * 3);

//4 sum extended values
var iSum=(a1+b1+c1+d1+e1+f1+g1+h1+i1+j1+k1+l1);

//5&6 divide sum by 10, returning the remainder
var iDiv=(iSum % 10);

//7 multiply remainder by 10
var By10=(iDiv * 10);

//8 subtract remainder from step 7 from "10" (this is check digit -if remainder=0 check=0)
var CheckD=(10 - By10);

//"CheckD" is the calculated check digit to compare to user input.
if (CheckD != m)
{
ErrorAlert();
}

}

//If EANCheck is true allow them to tab to next field.
//If EANCheck is false, execute ErrorAlert() & set the focus back on the EAN field.
-->
</script>
</head>
<body>
<form id="caspioform">

EAN:<input type="text" name="InsertRecordEAN" id="InsertRecordEAN" size="30" onblur="EANCheck(this.value)">
<br>
Age: <input type="text" id="age" size="30"><br><br>
<br>
<script type="text/javascript">
<!--
//test to show initial variables are working, or not -guess not, this doesn't seem to work.
document.write(a);
document.write(b);
document.write(c);
document.write(d);
document.write(e);
-->
</script>
</form>
</body>
</html>

A1ien51
02-12-2009, 02:16 AM
Why the new thread? http://codingforums.com/showthread.php?t=157983

de1
02-17-2009, 03:54 PM
Why the new thread? http://codingforums.com/showthread.php?t=157983

This one works a bit differently, and should be easier -and no one had responded to the other one. Since I thought this would be easier to troubleshoot I was hoping I'd get some help and then apply the fixes to the ISBN verification -once I figured out how to handle the check digit that could be numerical or text -EAN's are all numerical.

Any idea why this one wouldn't work?

Thanks for asking!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum