2

I need a script that will test an input field's contents to see if it contains an ISBN. I found a few examples of this, but none of them strip the dashes. I need this to happen or my search results don't work. I have the else part of the script working if the field doesn't have an ISBN, but can't get the ISBN test to work. Thank you in advance for any help!

function search() {
    var textboxdata = $('#search').val();
        if (textboxdata contains an ISBN number, strip it of dashes and) {
            // perform ISBN search
            document.location.href = "http://myurl?search=" + textboxdata;
        }
        else { //perform other search
        }
 }
lmg
  • 79
  • 1
  • 2
  • 5

4 Answers4

7

Based on the algorithms given in the Wikipedia article, here's a simple javascript function for validating 10- and 13-digit ISBNs:

var isValidIsbn = function(str) {

    var sum,
        weight,
        digit,
        check,
        i;

    str = str.replace(/[^0-9X]/gi, '');

    if (str.length != 10 && str.length != 13) {
        return false;
    }

    if (str.length == 13) {
        sum = 0;
        for (i = 0; i < 12; i++) {
            digit = parseInt(str[i]);
            if (i % 2 == 1) {
                sum += 3*digit;
            } else {
                sum += digit;
            }
        }
        check = (10 - (sum % 10)) % 10;
        return (check == str[str.length-1]);
    }

    if (str.length == 10) {
        weight = 10;
        sum = 0;
        for (i = 0; i < 9; i++) {
            digit = parseInt(str[i]);
            sum += weight*digit;
            weight--;
        }
        check = (11 - (sum % 11)) % 11
        if (check == 10) {
            check = 'X';
        }
        return (check == str[str.length-1].toUpperCase());
    }
}
Derek Kurth
  • 1,771
  • 1
  • 19
  • 19
  • @Bruno It worked for me using your test ISBN (which appears to be valid). I opened the console window in Chrome developer tools, pasted the code for the isValidIsbn function given above, then ran isValidIsbn("9781906230166") and got the result "true". – Derek Kurth Jul 01 '16 at 14:53
  • This code only checks the check-digit. This does not mean the ISBN is valid. (Test: `"978-1-906-230-166"` Should return `false` whilst `"978-1-906230-16-6"` should return `true`. Maybe rename this function to `checkDigit()`? – Bruno Jul 03 '16 at 02:42
  • @Bruno Good point! I was only interested in the checksum, but according to the spec, "The elements must each be separated clearly by hyphens or spaces when displayed in human readable form. ISBN 978-0-571-08989-5 or ISBN 978 0 571 08989 5". It also says, "Note: The use of hyphens or spaces has no lexical significance and is purely to enhance readability." So it looks like the ISBN is *valid* without hyphens, but you should include the hyphens when displaying an ISBN to a user. (see section 4 of https://www.isbn-international.org/sites/default/files/ISBN%20Manual%202012%20-corr.pdf) – Derek Kurth Jul 04 '16 at 12:26
  • That is awesome, I did not know that. But it is still just a check digit. For example this number will return `True`: `isValidIsbn("8781906230166")`; – Bruno Jul 05 '16 at 05:07
  • Since ISBN 13's currently always start with 978, adding a check for /^978/ to the ISBN 13 check would help reject an number of scanner errors when scanning a book's barcode. – JohnK Feb 09 '18 at 18:58
  • @JohnK That is probably a good idea. A few places are also starting to use the 979 prefix (see: https://en.wikipedia.org/wiki/List_of_ISBN_identifier_groups#Identifiers_of_the_979-_prefix) – Derek Kurth Feb 12 '18 at 01:14
  • Nice solution. Can I use this in a project under GPL? – Denis Apr 25 '19 at 16:07
  • @Denis, thanks! All code posted to Stack Overflow is licensed under Creative Commons Attribution-Share Alike (see here: https://stackoverflow.com/help/licensing). – Derek Kurth Apr 29 '19 at 13:40
  • @DerekKurth, yes, I know. The problem is that my project is under GPLv3, which is not compatible with CC-BY-SA 3.0 (CC-BY-SA 4.0 would be compatible though.). That's why I was asking for explicit permission. (Even if Code on Stack Overflow is usually CC-BY-SA you, as the creator, can still license it under different terms.) In the meantime, I have put a working solution together, but yours is much more elegant... – Denis Apr 30 '19 at 11:45
  • @Denis Interesting! Well, I don't know the implications of using the different licenses, but you (and anyone else) are welcome to use my solution, as far as I'm concerned. – Derek Kurth May 01 '19 at 16:36
2

There is also a js library available for checking ISBN10 and ISBN13 formatting: isbnjs as well as isbn-verify


Edit 2/2/17 - previous link was to Google Code, some updated current links: - npm for isbn-verify - npm for isbnjs - Github project

Andrew Cafourek
  • 431
  • 4
  • 11
1

Take a look at this Wikipedia article:

http://en.wikipedia.org/wiki/International_Standard_Book_Number

Should give you some insight into how to validate an ISBN number.

William Troup
  • 12,739
  • 21
  • 70
  • 98
1

Derek's code fails for this ISBN ==> "0756603390"

It's because the check digit will end up as 11.

incorrect == > check = 11 - (sum % 11); correct ==> check = (11 - (sum % 11)) %11;

I tested the new code against 500 ISBN10s.

Farnam
  • 11
  • 2