function IsWrongIP(Ip: string): Boolean;
const
Z = ['0'..'9', '.'];
var
I, J, P: Integer;
W: string;
begin
Result := False;
if (Length(Ip) > 15) or (Ip[1] = '.') then Exit;
I := 1;
J := 0;
P := 0;
W := '';
repeat
if (Ip[I] in Z) and (J <>then
begin
if Ip[I] = '.' then
begin
Inc(P);
J := 0;
try
StrToInt(Ip[I + 1]);
except
Exit;
end;
W := '';
end
else
begin
W := W + Ip[I];
if (StrToInt(W) > 255) or (Length(W) > 3) then Exit;
Inc(J);
end;
end
else
Exit;
Inc(I);
until I > Length(Ip);
if P <>then Exit;
Result := True;
end;
6 comments:
function IsWrongIP(ip: string): Boolean;
var
z, i: integer;
st: array[1..3] of byte;
const
ziff = ['0'..'9'];
begin
st[1] := 0;
st[2] := 0;
st[3] := 0;
z := 0;
Result := False;
for i := 1 to Length(ip) do
if ip[i] in ziff then
else
begin
if ip[i] = '.' then
begin
Inc(z);
if z < 4 then st[z] := i
else
begin
IsWrongIP := True;
beep;
Exit;
end;
end
else
begin
IsWrongIP := True;
Exit;
end;
end;
if (z <> 3) or (st[1] < 2) or (st[3] = Length(ip)) or (st[1] + 2 > st[2]) or
(st[2] + 2 > st[3]) or (st[1] > 4) or (st[2] > st[1] + 4) or (st[3] > st[2] + 4) then
begin
IsWrongIP := True;
Exit;
end;
z := StrToInt(Copy(ip, 0,st[1] - 1) );
if (z > 255) or (ip[1] = '0') then
begin
IsWrongIP := True;
Exit;
end;
z := StrToInt(Copy(ip, st[1] + 1, st[2] - st[1] - 1));
if (z > 255) or ((z <> 0) and (ip[st[1] + 1] = '0')) then
begin
IsWrongIP := True;
Exit;
end;
z := StrToInt(Copy(ip, st[2] + 1, st[3] - st[2] - 1));
if (z > 255) or ((z <> 0) and (ip[st[2] + 1] = '0')) then
begin
IsWrongIP := True;
Exit;
end;
z := StrToInt(Copy(ip, st[3] + 1, Length(ip) - st[3]));
if (z > 255) or ((z <> 0) and (ip[st[3] + 1] = '0')) then
begin
IsWrongIP := True;
Exit;
end;
end;
It's simpler:
function is_it_ip(S:String): Boolean;
var i,dot: Integer;
begin
Result := false;
if Length(S) = 0 then exit;
dot := 0;
for i := 1 to Length(S) do
begin
if S[i] = '.' then dot := dot+1;
if not ( S[i] in ['0'..'9','.']) then dot := 255;
end;
if (dot=3) then Result := true;
end;
Pretty good coding, you code like I do. One bug though . . . This only checks for valid numbers and counts dots. You could enter a number higher than 255 and it will still validate as true. Here's the way I do it:
Function ValidateIP(IP4: string): Boolean; // Coding by Dave Sonsalla
Var
Octet : String;
Dots, I : Integer;
Begin
IP4 := IP4+'.'; //add a dot. We use a dot to trigger the Octet check, so need the last one
Dots := 0;
Octet := '0';
For I := 1 To Length(IP4) Do
Begin
If IP4[I] in ['0'..'9','.'] Then
Begin
If IP4[I] = '.' Then //found a dot so inc dots and check octet value
Begin
Inc(Dots);
If (length(Octet) =1) Or (StrToInt(Octet) > 255) Then Dots := 5; //Either there's no number or it's higher than 255 so push dots out of range
Octet := '0'; // Reset to check the next octet
End // End of IP4[I] is a dot
Else // Else IP4[I] is not a dot so
Octet := Octet + IP4[I]; // Add the next character to the octet
End // End of IP4[I] is not a dot
Else // Else IP4[I] Is not in CheckSet so
Dots := 5; // Push dots out of range
End;
Result := (Dots = 4) // The only way that Dots will equal 4 is if we passed all the tests
end;
I use this one
function IsWrongIP(ip: string): Boolean;
var s: TStringDynArray;
begin
result:=true;
try
s:=SplitString(ip, '.');
if length(s)<>4 then exit;
if (strtoint(s[0])>255) or
(strtoint(s[1])>255) or
(strtoint(s[2])>255) or
(strtoint(s[3])>255) then exit;
except
exit;
end;
result:=false;
end;
function IsWrongIP(ip: string): Boolean;
begin
if Trim(ip)='' then
Result:= True
else begin
Result:= inet_addr(PAnsiChar(ip)) = INADDR_NONE;
end;
end;
What is wrong is the name of your function... :)
Never use negative names for methods and variables.
In this very case, "IsValidIP" is better than "IsWrongIP" since, to check the validity of an IP address, the following
if IsValidIP(...) then ...
is much clearer than
if not IsWrongIP(...) then ...
Don't you think?
Post a Comment