de1
02-05-2009, 02:52 PM
Hi everyone,
I've been trying to teach myself javascript over the last few weeks in order to build a script that can verify the check digit on ISBN numbers. This is what I have so far, which of course is not working. 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 ISBN number in a text field (called "InsertRecordISBN") and leaves this field (onblur, I believe) I'd like the script to run, checking to see if it's a valid ISBN number. If the number is invalid I'd like an alert box to pop up saying so, and when they click OK it highlights the incorrect value, not letting them leave that field until it's correct.
My sad little script:
<html>
<head>
<script type="text/javascript">
<!--
function ErrorAlert()
{
document.getElementByName("InsertRecordISBN").onblur = alert("The ISBN you entered is incorrect, please re-enter it.")
document.getElementByName("InsertRecordISBN").focus();
}
-->
</script>
<script type="javascript">
<!--
//parse ISBN
var a = document.getElementByName("InsertRecordISBN").value.substring(0,1)
var b = document.getElementByName("InsertRecordISBN").value.substring(1,2)
var c = document.getElementByName("InsertRecordISBN").value.substring(2,3)
var d = document.getElementByName("InsertRecordISBN").value.substring(3,4)
var e = document.getElementByName("InsertRecordISBN").value.substring(4,5)
var f = document.getElementByName("InsertRecordISBN").value.substring(5,6)
var g = document.getElementByName("InsertRecordISBN").value.substring(6,7)
var h = document.getElementByName("InsertRecordISBN").value.substring(7,8)
var i = document.getElementByName("InsertRecordISBN").value.substring(8,9)
var j = document.getElementByName("InsertRecordISBN").value.substring(9,10)
//multiply each digit against weighting factors to get extended value
var a1 = a * 10
var b1 = b * 9
var c1 = c * 8
var d1 = d * 7
var e1 = e * 6
var f1 = f * 5
var g1 = g * 4
var h1 = h * 3
var i1 = i * 2
//sum extended values
var iSum = sum(a1,b1,c1,d1,e1,f1,g1,h1,i1)
//divide sum by 11, returning the remainder
var iDiv = iSum % 11
//multiply remainder by 10, and round up
var By10 = iDiv * 10
//round By10 up
var Round = Math.round(By10)
//subtract By10 from 11
var CheckD = 11 - By10
function Isbn10Check()
{
//"CheckD" is check dig unless = 10, then it is "X" -if 11 it is zero.
if (CheckD == 10)
{
CheckD = "X"
}
if (CheckD == 11)
{
CheckD = "0"
}
//compare calculated check digit against check digit supplied
if (Check.value == j)
{
true;
}
else
{
ErrorAlert()
}
}
//If ISBNCheck is true allow them to tab to next field.
//If ISBNCheck is false, execute ErrorAlert() & set the focus back on the ISBN field.
-->
</script>
<body>
<form id="caspioform">
ISBN:<input type="text" name="InsertRecordISBN" id="InsertRecordISBN" size="30" onblur="Isbn10Check(this.value)"><br>
Age: <input type="text" id="age" size="30"><br><br>
<br>
</form>
<script type="text/javascript">
<!--
//test to show initial variables are working, or not -this doesn't seem to work.
document.write("test")
document.write(a)
document.write(b)
document.write(c)
document.write(d)
document.write(e)
</script>
</head>
</body>
</html>
I've been trying to teach myself javascript over the last few weeks in order to build a script that can verify the check digit on ISBN numbers. This is what I have so far, which of course is not working. 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 ISBN number in a text field (called "InsertRecordISBN") and leaves this field (onblur, I believe) I'd like the script to run, checking to see if it's a valid ISBN number. If the number is invalid I'd like an alert box to pop up saying so, and when they click OK it highlights the incorrect value, not letting them leave that field until it's correct.
My sad little script:
<html>
<head>
<script type="text/javascript">
<!--
function ErrorAlert()
{
document.getElementByName("InsertRecordISBN").onblur = alert("The ISBN you entered is incorrect, please re-enter it.")
document.getElementByName("InsertRecordISBN").focus();
}
-->
</script>
<script type="javascript">
<!--
//parse ISBN
var a = document.getElementByName("InsertRecordISBN").value.substring(0,1)
var b = document.getElementByName("InsertRecordISBN").value.substring(1,2)
var c = document.getElementByName("InsertRecordISBN").value.substring(2,3)
var d = document.getElementByName("InsertRecordISBN").value.substring(3,4)
var e = document.getElementByName("InsertRecordISBN").value.substring(4,5)
var f = document.getElementByName("InsertRecordISBN").value.substring(5,6)
var g = document.getElementByName("InsertRecordISBN").value.substring(6,7)
var h = document.getElementByName("InsertRecordISBN").value.substring(7,8)
var i = document.getElementByName("InsertRecordISBN").value.substring(8,9)
var j = document.getElementByName("InsertRecordISBN").value.substring(9,10)
//multiply each digit against weighting factors to get extended value
var a1 = a * 10
var b1 = b * 9
var c1 = c * 8
var d1 = d * 7
var e1 = e * 6
var f1 = f * 5
var g1 = g * 4
var h1 = h * 3
var i1 = i * 2
//sum extended values
var iSum = sum(a1,b1,c1,d1,e1,f1,g1,h1,i1)
//divide sum by 11, returning the remainder
var iDiv = iSum % 11
//multiply remainder by 10, and round up
var By10 = iDiv * 10
//round By10 up
var Round = Math.round(By10)
//subtract By10 from 11
var CheckD = 11 - By10
function Isbn10Check()
{
//"CheckD" is check dig unless = 10, then it is "X" -if 11 it is zero.
if (CheckD == 10)
{
CheckD = "X"
}
if (CheckD == 11)
{
CheckD = "0"
}
//compare calculated check digit against check digit supplied
if (Check.value == j)
{
true;
}
else
{
ErrorAlert()
}
}
//If ISBNCheck is true allow them to tab to next field.
//If ISBNCheck is false, execute ErrorAlert() & set the focus back on the ISBN field.
-->
</script>
<body>
<form id="caspioform">
ISBN:<input type="text" name="InsertRecordISBN" id="InsertRecordISBN" size="30" onblur="Isbn10Check(this.value)"><br>
Age: <input type="text" id="age" size="30"><br><br>
<br>
</form>
<script type="text/javascript">
<!--
//test to show initial variables are working, or not -this doesn't seem to work.
document.write("test")
document.write(a)
document.write(b)
document.write(c)
document.write(d)
document.write(e)
</script>
</head>
</body>
</html>