function IsLegalIp(Str: string): Boolean;
var
I, K, DotCnt : Integer;
Num: string;
Arr: Array [1..4] of string;
begin
Result := False;
DotCnt := 0;
//由'0'..'9', '.'组成
For I := 1 to Length(Str) do
begin
if Not (Str[I] in ['0'..'9', '.']) then
Exit
else
if Str[I] = '.' then
inc(DotCnt);
end;
//点分隔符号数量应该=3
if DotCnt
3 then Exit;
For K := 1 to 3 do
begin
I := Pos('.', Str);
Num := Copy(Str, 1, I - 1);
Delete(Str, 1, I);
Arr[K] := Num;
end;
Arr[4] := Str;
try
DotCnt := 0;
For I := 1 to 4 do
begin
K := StrToInt(Arr[I]);
//每字节的值应介于0~255之间
if ((K >= 0) and (K <= 255)) then
Inc(DotCnt);
end;
//四个字节都满足0~255之间,则合法
if(DotCnt = 4) then
Result := True;
except
end;
end; |