Issues

ZF-1277: adding validation of isbn

Issue Type: New Feature Created: 2007-04-14T12:12:12.000+0000 Last Updated: 2008-09-11T02:33:56.000+0000 Status: Resolved Fix version(s): - 1.5.0 (17/Mar/08)

Reporter: Marc Bennewitz (GIATA mbH) (mben) Assignee: Andries Seutens (andries) Tags: - Zend_Validate

Related issues: Attachments: - Zend_Validate_Isbn.php

Description

http://en.wikipedia.org/wiki/Isbn http://www.isbn-international.org/

Comments

Posted by Gavin (gavin) on 2007-04-16T13:10:34.000+0000

I expect people will be open to adding this. A code submission would be appreciated :)

Posted by Jan Pieper (jpieper) on 2007-04-16T16:28:37.000+0000

I think it should be added like Zend_Validate_Isbn but how we do the difference between ISBN-10 and ISBN-13 or is ISBN-10 deprecated enough that we can lose sight of? Or should we add Zend_Validate_Isbn_13Digits and Zend_Validate_Isbn_10Digits? DonĀ“t know.

What do you think?

Here a small quick and dirty solution for ISBN-13 and ISBN-10 without formatting (and no check for valide input):

<pre class="highlight"><?php

function validateIsbn10($isbn)
{   
    $remainder = $isbn{9};
    $checksum  = 0;
    
    for ($i=10, $a=0; $i>1; $i--, $a++)
    {
        $num       = $isbn{$a};
        $checksum += $num * $i;
    }
    
    return ((11 - ($checksum % 11)) == $remainder);
}

function validateIsbn13($isbn)
{
    $remainder = $isbn{12};
    $checksum  = 0;
    
    for ($i=0; $i<12; $i++)
    {
        $multi     = (($i % 2) == 1) ? 1 : 3;
        $num       = $isbn{$i};
        $checksum += $num * $multi;
    }
    
    return ((10 - ($checksum % 10)) == $remainder);
}

$isbn = '9783866400016';
var_dump(validateIsbn13($isbn)); // true

$isbn = '0306406153';
var_dump(validateIsbn10($isbn)); // true

?>

Posted by Marc Bennewitz (GIATA mbH) (mben) on 2007-04-17T03:43:47.000+0000

The Zend_Validate_Isbn can configure before which isbn versions are alowed.

<pre class="highlight">
$isbn13 = '9783866400016';
$isbn10 = '0306406153';

$validator = new Zend_Validate_Isbn();
$validator ->allowVersion(10);
$validator->isValid($isbn10); // true
$validator->isValid($isbn13); // false

$validator ->allowVersion(13);
$validator->isValid($isbn10); // true
$validator->isValid($isbn13); // true

Posted by Jan Pieper (jpieper) on 2007-04-17T04:38:41.000+0000

Attached propsal for Zend_Validate_Isbn.

<pre class="highlight">$isbn13 = '9783866400016';
$isbn10 = '0306406152';

$oIsbn = new Zend_Validate_Isbn;
$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN13);

var_dump($oIsbn->isValid($isbn13)); // true
var_dump($oIsbn->isValid($isbn10)); // false

$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN10);

var_dump($oIsbn->isValid($isbn13)); // false
var_dump($oIsbn->isValid($isbn10)); // true

Posted by Jan Pieper (jpieper) on 2007-04-17T04:53:50.000+0000

Attached new version of propsal (small changes).

<pre class="highlight">require_once 'Zend/Filter/Digits.php';

$isbn13  = '9783866400016';
$isbn10  = '0306406152';
$isbn13s = '978-3-866-40001-6';
$isbn10s = '0-306-40615-2';

$oDigits = new Zend_Filter_Digits();
$isbn13sc = $oDigits->filter($isbn13s);
$isbn10sc = $oDigits->filter($isbn10s);

$oIsbn = new Zend_Validate_Isbn;
$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN13);

var_dump($oIsbn->isValid($isbn13)); // true
var_dump($oIsbn->isValid($isbn10)); // false
var_dump($oIsbn->isValid($isbn13s)); // false
var_dump($oIsbn->isValid($isbn10s)); // false
var_dump($oIsbn->isValid($isbn13sc)); // true
var_dump($oIsbn->isValid($isbn10sc)); // false

$oIsbn->allowVersion(Zend_Validate_Isbn::ISBN10);

var_dump($oIsbn->isValid($isbn13)); // false
var_dump($oIsbn->isValid($isbn10)); // true
var_dump($oIsbn->isValid($isbn13s)); // false
var_dump($oIsbn->isValid($isbn10s)); // false
var_dump($oIsbn->isValid($isbn13sc)); // false
var_dump($oIsbn->isValid($isbn10sc)); // true

Posted by Marc Bennewitz (GIATA mbH) (mben) on 2007-04-17T10:46:33.000+0000

In Zend_Validate_Isbn_V3.php: It can test isbn numbers with valid string separators " " and "-" and the isbn can start wth "isbn" because it is a valid isbn and must not filtered to digits manuely. secondary it can test a isbn by all versions in one call.

Posted by Jan Pieper (jpieper) on 2007-04-17T12:35:16.000+0000

I like that you implemented sth. to validate the isbn generally and not only ISBN-10 or ISBN-13.

I think we should split up filtering and validation. We can use filtering in validation but filtering should be in a seperate class (e.g. Zend_Filter_Isbn).

Posted by Bill Karwin (bkarwin) on 2007-04-17T19:20:33.000+0000

Assign to Darby.

Posted by Andries Seutens (andries) on 2007-07-03T09:26:23.000+0000

There's a proposal related to this "issue", which can be found here.

Personally, I don't like to see ISBN-10 and ISBN-13 wrapped in one class, they should be 2 seperate validators if you ask me. I also think that these validators should go under the Zend_Validate_Barcode_* namespace.

Posted by Darby Felton (darby) on 2007-08-24T08:56:20.000+0000

Reassigning to [~andries].

Posted by Dave Hall (skwashd) on 2008-08-17T06:38:18.000+0000

ISBN-10 is now dead, the transition period ended on 1 Jan 2007.

ISBN-13 validation support is already available in ZF via the barcode validator as ISBN-13 is the same as a "bookland" EAN-13 barcode.

I think this ticket should be closed.

Posted by Adam Kusmierz (raistlin) on 2008-09-11T01:32:07.000+0000

There is isbn validator at Zend Validate Barcode.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts