Einzelnen Beitrag anzeigen

uoeb7gp
(Gast)

n/a Beiträge
 
#6

Re: ip auf gültigkeit prüfen

  Alt 13. Nov 2009, 12:32
Hi, hab für Produktiv Systeme seit Jahren folgendes in Verwendung:

Delphi-Quellcode:
function TMainClass.CheckIPEx(s: string): Boolean;
var
  s1, s2, s3, s4: String;
  e, v, i, j, sum: Integer;
  bcLen: integer;
  ix: array[1..3] of integer;
begin
  result := false;

  if (s = '0.0.0.0') then EXIT;

  j := 0;
  bcLen := Length(s);
  for i:= 1 to bcLen do begin
      if s[i] = '.then begin
         inc(j);
         ix[j] := i;
      end;
  end;
  if j <> 3 then EXIT;

  s1 := copy(s, 1, ix[1] - 1);
  s2 := copy(s, ix[1] + 1, ix[2] - ix[1] - 1);
  s3 := copy(s, ix[2] + 1, ix[3] - ix[2] - 1);
  s4 := copy(s, ix[3] + 1, bcLen);

  sum := 0;

  Val(s1, v, e);
  if (e <> 0) or (v > 255) or (v < 0) or ((Length(s1) > 1) and (s1[1] = '0')) then EXIT;
  sum := sum + v;

  Val(s2, v, e);
  if (e <> 0) or (v > 255) or (v < 0) or ((Length(s2) > 1) and (s2[1] = '0')) then EXIT;
  sum := sum + v;


  Val(s3, v, e);
  if (e <> 0) or (v > 255) or (v < 0) or ((Length(s3) > 1) and (s3[1] = '0')) then EXIT;
  sum := sum + v;


  Val(s4, v, e);
  if (e <> 0) or (v > 255) or (v < 0) or ((Length(s4) > 1) and (s4[1] = '0')) then EXIT;
  sum := sum + v;

  if sum <> 0 then
    result := true;
end;
  Mit Zitat antworten Zitat