Validate an ISSN using Perl or JavaScript
Posted: 12 June 2009
Last week I wrote about validating an ISBN, so this week I thought I would post the code to validate an ISSN.
There are only a few minor differences between validating an ISSN and ISBN.
Validate ISSN in Perl
sub valid_issn {
my $issn = $_[0];
$issn =~ s/[^\dX]//gi;
return if length($issn) != 8;
my $sum = 0;
my @chars = split('', $issn);
$chars[7] = 10 if uc($chars[7]) eq 'X';
for (my $char = 0; $char < @chars; $char++) {
$sum += (8-$char) * $chars[$char];
}
return (($sum % 11) == 0);
}
Validate ISSN in JavaScript
function isValidISSN (issn) {
issn = issn.replace(/[^\dX]/gi, '');
if(issn.length != 8){
return false;
}
var chars = issn.split('');
if(chars[7].toUpperCase() == 'X'){
chars[7] = 10;
}
var sum = 0;
for (var i = 0; i < chars.length; i++) {
sum += ((8-i) * parseInt(chars[i]));
};
return ((sum % 11) == 0);
}Post a comment
Comment Guidelines
- Have no more than 2 links, otherwise your comment will be flagged as spam.
- Links are automagically generated.
- <em>text</em> to make text italic.
- <strong>text</strong> to make text bold.
JavaScript needs to be enabled to comment.
Your comments
No comments have been made. Why not be the first?