1 # Copyright 2008,2009 Interchange Development Group
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 CodeDef isbn OrderCheck 1
9 CodeDef isbn Description ISBN-10/ISBN-13 check digit verification
10 CodeDef isbn Routine <<EOR
12 my($ref, $var, $val, $msg) = @_;
15 if ($msg =~ s/^\s*(10|13)\s*//) {
19 $val =~ s/[^\dXx]//g; # weed out non-digits
21 my @digits = split("", $val);
29 return (0, $var, errmsg("'%s' not a valid isbn-13 number", $val));
31 for(my $i=10; $i > 0; $i--) {
32 my $d = $digits[10 - $i];
38 return (undef, $var, errmsg("'%s' not a valid isbn number", $val));
43 return ( $sum%11 ? 0 : 1, $var, '' );
44 } elsif (@digits == 13) {
47 return (0, $var, errmsg("'%s' not a valid isbn-10 number", $val));
49 for (my $i = 0; $i < 12; $i++) {
51 $sum += 3 * $digits[$i];
58 if ($modulo = $sum % 10) {
59 $check_digit = 10 - $modulo;
62 if (pop(@digits) == $check_digit) {
63 # verification successful
69 return (undef, $var, errmsg("'%s' not a valid isbn number", $val));