有没有办法在存储到数据库之前检查 ISBN 号码是否有效

发布于 2024-10-03 21:10:32 字数 89 浏览 11 评论 0原文

我想知道是否有一个网络服务或数据库可以让我们在将用户输入存储到数据库之前以编程方式检查用户输入是否是有效的 ISBN 编号。或者是否有一种算法可以让程序员做到这一点

I was wonder is a there a web service or a database out there that let us programmatically check if the user input is an valid ISBN number before storing it in the database. Or is there a algorithm that allow the programmer to do that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

高冷爸爸 2024-10-10 21:10:32

如果查看维基百科页面,他们有一个简单的算法来确保 ISBN 有效:

bool is_isbn_valid(char digits[10]) {
    int i, a = 0, b = 0;
    for (i = 0; i < 10; i++) {
        a += digits[i];  // Assumed already converted from ASCII to 0..9
        b += a;
    }
    return b % 11 == 0;
}

当然,这并不能确保它是真实的,只是可能;)

编辑:这包括 ISBN 13 规范:(未经测试,并且与维基百科函数使用相同的伪代码)

bool is_valid_isbn(char isbn[]) {
    int sum = 0;
    if(isbn.length == 10) {
        for(int i = 0; i < 10; i++) {
            sum += i * isbn[i]; //asuming this is 0..9, not '0'..'9'
        }

        if(isbn[9] == sum % 11) return true;
    } else if(isbn.length == 13) {

        for(int i = 0; i < 12; i++) {
            if(i % 2 == 0) {
                sum += isbn[i]; //asuming this is 0..9, not '0'..'9'
            } else {
                sum += isbn[i] * 3;
            }
        }

        if(isbn[12] == 10 - (sum % 10)) return true;
    }

    return false;
}

If look on the Wikipedia page, they have a simple algorithm to ensure than an ISBN is valid:

bool is_isbn_valid(char digits[10]) {
    int i, a = 0, b = 0;
    for (i = 0; i < 10; i++) {
        a += digits[i];  // Assumed already converted from ASCII to 0..9
        b += a;
    }
    return b % 11 == 0;
}

Of course, this doesn't ensure that it's real, only possible ;)

EDIT: This includes the ISBN 13 spec: (It's untested, and in the same pseudo-code as the wikipedia function)

bool is_valid_isbn(char isbn[]) {
    int sum = 0;
    if(isbn.length == 10) {
        for(int i = 0; i < 10; i++) {
            sum += i * isbn[i]; //asuming this is 0..9, not '0'..'9'
        }

        if(isbn[9] == sum % 11) return true;
    } else if(isbn.length == 13) {

        for(int i = 0; i < 12; i++) {
            if(i % 2 == 0) {
                sum += isbn[i]; //asuming this is 0..9, not '0'..'9'
            } else {
                sum += isbn[i] * 3;
            }
        }

        if(isbn[12] == 10 - (sum % 10)) return true;
    }

    return false;
}
提赋 2024-10-10 21:10:32

您可以根据以下算法计算校验和: http://en.wikipedia.org/wiki/International_Standard_Book_Number #Check_digits。这并不能告诉您是否确实有一本书具有该编号,但它可以告诉您该编号是否正确。

You could calc the checksum according to this algorithm: http://en.wikipedia.org/wiki/International_Standard_Book_Number#Check_digits. That doesn't tell you if there is actually a book with that number but it tells you if the number is correct.

ゃ人海孤独症 2024-10-10 21:10:32

血淋淋的细节在这里。因此,您可以检查校验位并尝试推断国家(也许还有出版商)是什么,但我没有看到任何方法来检查是否有一本带有该编号的真实书籍

Gory details here. So you can check the check digit and have a go at deducing what the country (and maybe publisher) are, but I don't see any way of checking there's a real book with that number

娇俏 2024-10-10 21:10:32

要仅验证校验位,而不是与已知的书籍列表进行比较,您可以使用 Apache Commons ISBNValidator 类。

ISBNValidator validator = new ISBNValidator(); 
assertFalse(validator.isValid("12345678901234")); 
assertTrue(validator.isValid("0-201-63385-X"));

代码示例来自:
https:// /apache.googlesource.com/commons-validator/+/trunk/src/test/java/org/apache/commons/validator/ISBNValidatorTest.java

参考文献:
https://commons.apache .org/proper/commons-validator/apidocs/org/apache/commons/validator/routines/ISBNValidator.html

To validate just the checkdigit, and not compare against a known list of books, you could use the Apache Commons ISBNValidator class.

ISBNValidator validator = new ISBNValidator(); 
assertFalse(validator.isValid("12345678901234")); 
assertTrue(validator.isValid("0-201-63385-X"));

Code example from:
https://apache.googlesource.com/commons-validator/+/trunk/src/test/java/org/apache/commons/validator/ISBNValidatorTest.java

References:
https://commons.apache.org/proper/commons-validator/apidocs/org/apache/commons/validator/routines/ISBNValidator.html

扭转时空 2024-10-10 21:10:32

正如其他人在我的快速搜索中建议的那样,请尝试: idbndb

As others have suggested in my quick searches on SO try: idbndb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文