Neil Ang

Developer

A stunning likeness of Neil Ang
Hello world

Validate an ISSN using JavaScript

Posted on

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.

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);
}