Home » Tipps & Tricks » Algorithmen » Sonstiges » ISBN auf Gültigkeit prüfen

ISBN auf Gültigkeit prüfen

ISBN-Nummern enthalten eine Prüfsumme, mit deren Hilfe es möglich ist zu testen, ob eine gegebene ISBN korrekt ist oder ob vielleicht ein Tippfehler vorhanden ist.

Seit dem 1.1.2007 gilt für die ISBN-Nummern ein neues verbindliches Format (ISBN-13). Folgende Routine prüft, ob eine ISBN(-13)-Nummer valide ist:

{Funktion validiert ISBN-13
Gültig ab 1.1.2007
Wenn streng = True, wird die Voranstellung von 'ISBN ' und die korrekte
Verwendung der Bindestriche verlangt}
function ISBN(const s: string; streng: Boolean): Boolean;
var
  i, x, n: Integer;
  tmp: string;
begin
  Result := True;
  if (not streng) or ((Copy(s, 1, 5) = 'ISBN ') and (Length(s) = 22)
    and (s[9] = '-') and (s[21] = '-')) then
  begin
    for i := 1 to Length(s) do
      if s[i] in ['0'..'9'] then
        tmp := tmp + s[i]
      else
        if streng then
          if (s[i] = '-') then
            Inc(n)
          else
          if (i > 5) then
            Result := False;
    if (Result) and ((not streng) or (n = 4)) then
    begin
      if Length(tmp) = 13 then
      begin
        for i := 1 to 12 do
          if i div 2 = i/2 then
            x := x + StrToInt(tmp[i]) * 3
          else
            x := x + StrToInt(tmp[i]);
        if StrToInt(tmp[13])  ((10 - (x mod 10)) mod 10) then
          Result := False;
      end
      else
        Result := False;
    end
    else
      Result := False;
  end
  else
    Result := False;
end;

Über den Parameter ’streng‘ lässt sich kontrollieren, ob einfach nur die Prüfziffer, oder auch der korrekte Aufbau nach dem Muster ‚ISBN xxx-x-xxx-xxxxx-x‘ überprüft werden soll.

ISBN('ISBN 978-3-7657-2780-1', True) //liefert True zurück
ISBN('9783765727801', True) //liefert False zurück
ISBN('ISBN 978-3-7657-2780-1', False) //liefert True zurück
ISBN('9783765727801', False) //liefert True zurück

Auch die alte ISBN-10 enthält eine Prüfsumme. Eine solche ISBN-10 kann folgenermaßen validiert werden:

function ValidISBN(const aISBN: string): boolean;
var
  hNumber,
  hCheckDigit: string;
  hCheckValue,
  hCheckSum,
  Err: integer;
  i,Cnt: Word;
begin
  Result := false;
  hCheckDigit := Copy(aISBN, Length(aISBN), 1);
  {Get rest of ISBN, minus check digit and its hyphen}
  hNumber := Copy(aISBN, 1, Length(aISBN) - 2);
  {Length of ISBN remainder must be 11 and check digit
   between 9 and 9 or X}
  if (Length(hNumber)=11)and(Pos(hCheckDigit,
      '0123456789X') > 0) then
  begin
    {Get numeric value for check digit}
    if (hCheckDigit = 'X') then
      hCheckSum := 10
    else
      Val(hCheckDigit, hCheckSum, 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(hNumber[i], '0123456789') > 0) then
      begin
        Val(hNumber[i], hCheckValue, Err);
        {Algorithm for each character in ISBN remainder, Cnt
        is the nth character so processed}
        hCheckSum := hCheckSum + hCheckValue * (11 - Cnt);
        Inc(Cnt);
      end;
    end;
    {Verify final value is evenly divisible by 11}
    if (hCheckSum mod 11 = 0) then
      Result := true
    else
      Result := false;
  end;
end;

Weitere Informationen zu diesem Thema finden sich in der Wikipedia.