Diafol's Demos

Back To Index

<demo>Some Ideas for Testing ISBN Data</demo>

There follows a few ideas for testing or validating ISBN-10 and ISBN-13 data. It should be noted that the regex used in this page was taken from the regex library pages here by Muhammad - thank you Sir!

Example 1: Simple PHP Validation


Allowed Formats:

The Code

So here's the PHP code:

<?php
if($_POST)
{
	function checkISBN($isbn, $type=0)
	{
    		//Muhammad's regex checker
		$pattern = '/ISBN(-1(?:(0)|3))?:?\x20(\s)*[0-9]+[- ][0-9]+[- ][0-9]+[- ][0-9]*[- ]*[xX0-9]/';
		
		if(preg_match($pattern, $isbn) === false) return false;
		
        	//Strip non-integers - except for a possible 'X' at the end
		$isbn = preg_replace(array('/ISBN-13 /','/ISBN-10 /','/ISBN /', '/ISBN: /','/-/'), '', $isbn);
		
		$len = strlen ((string) $isbn);	
		
        	//Split isbn into single characters
		$isbnConv = str_split($isbn);
        
        	//If last char is X - make it 10
		if(strtoupper($isbnConv[$len-1]) == 'X') $isbnConv[$len-1] = 10;
		
        	//Set to automatic check if passed $type is '0'
		if(is_null($type) || (int) $type === 0) $type = $len; 
		
        	//Check for ISBN-10
		if($len === 10 && $type == 10)
		{
			$total = array_map(function($x, $y) { return $x * $y; },
					   $isbnConv, range(1,10));
			return (array_sum($total)%11 === 0) ? true : false;
        
        	//Check for ISBN-13    
		}elseif($len === 13 && $type == 13){
			$total = array_map(function($x, $y) { return $x * $y; },
					   $isbnConv, array(1,3,1,3,1,3,1,3,1,3,1,3,1));
			return (array_sum($total)%10 === 0) ? true : false;
		}else{
			return false;
		}
	}
	
	//Get the series of isbns to check from the textarea
	$isbnString = $_POST['isbns'];
	$isbns = explode("\n", $isbnString);
    	
        //Get the 'type' from the form radiobuttons: '0','10' or '13'
	$length = (int) $_POST['length'];
    
    	//Initialize result string 
	$result = '';
    
    	//Loop over checking function
	foreach($isbns as $isbn)
	{
		$isbn = trim($isbn);
		$result .= (checkISBN($isbn, $length)) ? "<p class='true'>$isbn <span>&#10004;</span><p>" : "<p class='false'>$isbn <span>&#10008;</span><p>";
	}
}
?>

And here's the HTML markup:

<?php if(isset($result) && $result) echo "<div id='isbnresult'><strong><span class='true'>Valid ISBN <span>&#10004;</span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span class='false'>Invalid ISBN <span>&#10008;</span></span></strong>$result</div>";?>

<form method="post">
	<label for = "isbns">List ISBNs separated by a new line:</label><br />
	<textarea id="isbns" name="isbns" rows="6" cols="50" placeholder="Put each ISBN on its own line - do not use commas or any other separator!"><?php if(isset($isbnString)) echo $isbnString;?></textarea>
    <p>Allowed Formats:</p>
    <ul>
    	<li>ISBN-13 978-3-642-11746-6</li>
        <li>ISBN 978-3-642-11746-6</li>
        <li>ISBN: 978-3-642-11746-6</li>
        <li>ISBN-10 3-642-11746-5</li>
        <li>ISBN 3-642-11746-5</li>
        <li>ISBN: 3-642-11746-5</li>
    </ul>
    <input type="radio" name="length" value="0" id="auto" <?php if((isset($length) && $length == '0') || !isset($length)) echo "checked";?> /><label for="auto">Auto</label>
    <input type="radio" name="length" value="10" id="ten" <?php if(isset($length) && $length == '10') echo "checked";?> /><label for="ten">ISBN 10 Only</label>
    <input type="radio" name="length" value="13" id="teen" <?php if(isset($length) && $length == '13') echo "checked";?> /><label for="teen">ISBN 13 Only</label>
    <input type="submit" value="Test" id="sub" name="sub" />
</form>

And a bit of CSS:

#isbnresult{
	border: 1px solid #999;
	padding: 10px;
	background-color:#eeeeee;
	font-size: 0.8rem;
	margin-bottom: 1.2rem;	
}
.true span{
	color: green;
}
.false span{
	color: red;
}

Example 2: Simple jQuery Validation


The Code

Here's the jQuery placed at the bottom of the page, just above the close body tag...

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Then within script tags...


//Fill textarea with example data
$('#fillExample2').click(function(e)
{
	e.preventDefault();
	$('#isbns2').val('ISBN-13 978-3-642-11746-6\nISBN 978-3-642-11746-6\nISBN: 978-3-642-11746-6\nISBN-10 3-642-11746-5\nISBN 3-642-11746-5\nISBN: 3-642-11746-5');
	$( "#isbns2" ).focus();
});

//Run code on textarea blur event
$('#isbns2').blur(function()
{
	startISBN();
});

//Run code on change radiobuttons
$('input[name=length2]').change(function()
{
	startISBN();	
	$( "#isbns2" ).focus();
}); 

//Run when change radiobuttons or blur textarea
function startISBN()
{
	var isbnString = $('#isbns2').val();
	var isbns = isbnString.split("\n");
	var length = $('input[name=length2]:checked').val(); 
	var result = '';
    
    //Kill function if textarea empty
	if($.trim(isbnString) == ''){ $('#isbnresult2').html(''); $('#isbnresult2').hide(); return;};
	
    //Loop over each isbn and add to the result var
    $.each(isbns, function(i,v)
	{
		isbn = $.trim(v);
		result += (isbnChecker(isbn, length)) ? "<p class='true'>"+v+" <span>&#10004;</span><p>" : "<p class='false'>"+v+" <span>&#10008;</span><p>";	
	});
    
	$('#isbnresult2').html(result);
	$('#isbnresult2').show();	
}

//Validate ISBN including the checkdigit
function isbnChecker(isbn, type)
{
	var isbnOrig = isbn;
	var pattern = /ISBN(-1(?:(0)|3))?:?\x20(\s)*[0-9]+[- ][0-9]+[- ][0-9]+[- ][0-9]*[- ]*[xX0-9]/g;
	var replacements = [/ISBN-13 /,/ISBN-10 /,/ISBN /,/ISBN: /,/-/g];
	var typeOrig = (type === undefined) ? 0 : type;
	var total;
	
	matches = isbnOrig.match(pattern);
	if(matches === null) return false;
	
	$.each(replacements,function(i,v)
	{
		isbnOrig = isbnOrig.replace(v,'');
	});
	
	var len = isbnOrig.length;	
	var isbnConv = isbnOrig.split("");
	
    //Check last character
	if(isbnConv[(len-1)].toUpperCase() == 'X') isbnConv[(len-1)] = 10;
	
	if(parseInt(typeOrig) === 0) typeOrig = len; 

	//Check ISBN-10	
	if(len === 10 && parseInt(typeOrig) == 10)
	{
		var isbn10 = [1,2,3,4,5,6,7,8,9,10];
		var sum = 0;
		for(var i=0; i< len; i++) {
			sum += isbnConv[i]*isbn10[i];
		}
		return (sum%11 === 0) ? true : false;
	
    //Check ISBN-13
	}else if(len === 13 && parseInt(typeOrig) == 13){
		var isbn13 = [1,3,1,3,1,3,1,3,1,3,1,3,1];
		var sum = 0;
		for(var i=0; i< len; i++) {
			sum += isbnConv[i]*isbn13[i];
		}
		return (sum%10 === 0) ? true : false;
	}else{
		return false;
	}
}

And here's the html form...

<div id='isbnresult2' style="display:none;"></div>

<form>
	<label for = "isbns2">List ISBNs separated by a new line:</label> <button id="fillExample2">Fill example data</button><br />
	<textarea id="isbns2" name="isbns2" rows="6" cols="50" placeholder="Put each ISBN on its own line - do not use commas or any other separator! Once finished, press 'TAB' to trigger onBlur() or change the radiobuttons to validate."></textarea>
    
    <input type="radio" name="length2" value="0" id="auto2" checked /><label for="auto2">Auto</label>
    <input type="radio" name="length2" value="10" id="ten2" /><label for="ten">ISBN 10 Only</label>
    <input type="radio" name="length2" value="13" id="teen2" /><label for="teen">ISBN 13 Only</label>
</form>