Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    de1
    de1 is offline
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ISBN verification

    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:

    Code:
    <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>

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    there are more than 10 numbers in an isbn, so your formula needs some tweaking.

    here is a regexp to make sure that at least the format is correct:

    Code:
    var isbnRx = /ISBN\s(?=[-0-9xX ]{13}$)(?:[0-9]+[- ]){3}[0-9]*[xX0-9]$/
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    de1
    de1 is offline
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, rnd_me.

    I believe you're thinking of EAN numbers, which are 13 characters long. This IS very helpful though, as I will be writing another version of this verification for EANs. Would you mind translating it into English for me? I know almost nothing about regular expressions.

    Thanks again!

    ...and if anyone can help me figure out why my variables aren't even picking up values it would be VERY much appreciated! This thing is driving me nuts! :\

  • #4
    de1
    de1 is offline
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Additional info: steps explained

    It just occurred to me that it would probably be helpful if I posted the steps I'm trying to accomplish, in English.

    How to create an ISBN 10 Check digit, with example

    Step 1 - Enter the 9 digit value needing a check digit below
    000714126

    Step 2 - Break the 9 character number apart, digit by digit (as per below in row 11)

    Step 3 - Multiply the individual digits by the weighting factor in row 12

    Digit Value 0 0 0 7 1 4 1 2 6
    Weighting Factors 10 9 8 7 6 5 4 3 2
    Extended Value 0 0 0 49 6 20 4 6 12

    Step 4 - Sum the nine extended values from Step 3 (in row 13 above)
    97

    Step 5 - Divide the sum calculated in Step 4 by 11
    8.818181818

    Step 6 - Calculate the remainder portion of Step 5
    0.818181818

    Step 7 - Multiply the remainder by 10 and then round up
    9

    Step 8 - Subtract the rounded remainder in Step 7 from 11
    2

    Step 9 - The value in Step 8 is your check digit unless it equals 10 in which case the check digit is "X" or 11 in which case the check digit is zero
    2

    Step 10 - Place the check digit at the end of your original 9 character number for your completed 10 Character ISBN
    0007141262


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •