Blog

Signals Blog

Validating CAS Numbers in JavaScript

CAS Registry Numbers are used extensively in chemistry and commerce to identify chemical substances. One reason is that CAS Numbers tolerate human data entry errors thanks to a built-in check digit. This article shows how to validate arbitrary CAS numbers in JavaScript through comparison of expected and actual check digits.

Anatomy of a CAS Number

A CAS Number is represented by a character sequence containing three groups of digits separated by hyphens. The first group contains two to seven digits. The second group contains two digits. The last group contains a single check digit.

For example, the CAS number for Imatinib, 152459-95-5, contains six digits in the first block, and a check digit of "5".

Chemical Abstracts Service reports that CAS numbers are sequentially assigned as they enter the CAS Registry. Unlike some chemical identifiers such as InChI Key, no other chemical meaning can be ascribed to a substance's CAS Number.

Calculating the Check Digit

CAS Numbers are represented generally as a sequence of digits numbered right-to-left, excluding the rightmost digit, which is labeled "R".

Ni ... N5 N4 N3 - N2 N1 - R

The check digit R is calculated by summing the product of all i times Ni and taking mod 10 of the result.

(ixNi + ... + 5xN5 + 4xN4 + 3xN3 + 2xN2 + N1) mod 10

We can write a JavaScript function that will calculate the CAS Number check digit.

Calculation of the CAS Number check digit
var getCheckDigit = function(cas) {
  var match = cas.match(/([0-9]{2,7})-([0-9]{2})-[0-9]/);
  var digits = (match[1] + match[2]).split('').reverse();
  var sum = 0;

  for (var i = 0; i < digits.length; i++) {
    sum += (i + 1) * parseInt(digits[i]);
  }

  return sum % 10;
};
 

Sample Page

Adding a bit of HTML results in a fully-functional CAS number validator.

CAS Number Validator
<!DOCTYPE html>
<html>
  <head>
    <title>CAS Number Validator</title>
    <script>
      var validator = {
        regex: /([0-9]{2,7})-([0-9]{2})-[0-9]/,
        getCheckDigit: function(cas) {
          var match = cas.match(this.regex);
          var digits = (match[1] + match[2]).split('').reverse();
          var sum = 0;

          for (var i = 0; i < digits.length; i++) {
            sum += (i + 1) * parseInt(digits[i]);
          }

          return sum % 10;
        },
        isValid: function(cas) {
          if (!cas.match(this.regex)) {
            return false;
          }

          return this.getCheckDigit(cas).toString() === cas.slice(-1);
        },
        validate: function() {
          var cas = document.querySelector('#cas').value;

          if (this.isValid(cas)) {
            alert(cas + ' is a valid CAS Number.');
          } else {
            alert(cas + ' is an invalid CAS Number.');
          }
        }
      }
    </script>
  </head>
  <body>
    <h1>CAS Number Validator</h1>
    <input id="cas" type="text">
    <input type="submit" value="Check CAS Number" onclick="validator.validate()">
  </body>
</html>