Validate ISBN codes  

Send By: Q3 Team
Web : http://www.q3.nu
Email: dlib@q3.nu
Date: 16/07/99

Tip accessed 1543 times

 


The codes ISBN allows us to identify any book. It is as their registration. They have a defined format, which includes a control digit to validate it. Look for it in any of your books.

An example of ISBN:


                               3-88053-002-5




It is compound for:
Group : 3Publisher : 88053Book : 002Check Digit : 5


Now, how is calculated the control digit?


With the previous ISBN example:
ISBN:3880530025
Multiplier:10987654321
Product:30+72+64+0+30+15+0+0+04+05=220


As you see, it is to multiply each digit of the ISBN for a factor that depends on their position, adding all the results. At the end, if the result is divisible for 11, the ISBN is valid.

Here you have a function that returns if a ISBN is valid or not:


   function IsISBN(ISBN: String): Boolean;
   var
     Number, CheckDigit: String;
     CheckValue, CheckSum, Err: Integer;
     i, Cnt: Word;
   begin
     {Get check digit}
     CheckDigit := Copy(ISBN, Length(ISBN), 1);
     {Get rest of ISBN, minus check digit and its hyphen}
     Number := Copy(ISBN, 1, Length(ISBN) - 2);
     {Length of ISBN remainder must be 11 and check digit between 9 and 9 or X}
     if (Length(Number) = 11) and (Pos(CheckDigit, '0123456789X') > 0) then
       begin
       {Get numeric value for check digit}
       if (CheckDigit = 'X') then
         CheckSum := 10
       else
         Val(CheckDigit, CheckSum, Err);
       {Iterate through ISBN remainder, applying decode algorithm}
       Cnt := 1;
       for i := 1 to 12 do begin
         {Act only if current character is between "0" and "9" to exclude hyphens}
         if (Pos(Number[i], '0123456789') > 0) then begin
           Val(Number[i], CheckValue, Err);
           {Algorithm for each character in ISBN remainder, Cnt is the nth
           character so processed}
           CheckSum := CheckSum + CheckValue * (11 - Cnt);
           Inc(Cnt);
         end;
       end;
       {Verify final value is evenly divisible by 11}
       if (CheckSum MOD 11 = 0) then
         IsISBN := True
       else
         IsISBN := False;
     end
     else
       IsISBN := False;
   end;



A call example:


  	If IsISBN ('3880530025') then ShowMessage('ISBN válido') else ShowMessage('ISBN invalido...');