function IsIp(Str: string): Boolean;
var
I, K, DotCnt : Integer;
Num: string;
Arr: Array [1..4] of string;
begin
Result := False;
DotCnt := 0;
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;
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]);
if ((K >= 0) and (K <= 255)) then
Inc(DotCnt);
end;
if(DotCnt = 4) then
Result := True;
except
end;
end;
function IsValidIP(astrIP:String):Boolean;
var
iCount:Integer;
iIPLength:Integer;
iFieldLength:Integer;
iFieldStart:Integer;
iDotCount:Integer;
strTemp:String;
begin
iIPLength:=Length(astrIP);
if (iIPLength>15)or(iIPLength<7)then
begin
Result:=False;//合法IPv4长度在7和15之间
Exit;
end;
iDotCount:=0;
iFieldLength:=0;
iFieldStart:=1;
for iCount:=1 to iIPLength do
begin
case astrIP[iCount] of
"0","1","2","3","4","5","6","7","8","9":
begin
iFieldLength:=iFieldLength+1;
if (3<iFieldLength) then
begin
Result:=False;//IP的单域长度超过3
Exit;
end;
end;
"."
begin
if 0=iFieldLength then
begin
Result:=False;//"."在开头或连续两个".",或在结尾
Exit;
end;
strTemp:=copy(astrIp,iFieldStart,iCount-iFieldStart);
if(255<StrToInt(strTemp))then
begin
Result:=False;//单域值>255
Exit;
end;
iDotCount:=iDotCount+1;
if 3<iDotCount then
begin
Result:=False;//超过4个域
Exit;
end;
iFieldLength:=0;
iFieldStart:=iCount+1;
end;
else
begin
Result:=False;//非法字符
Exit;
end;
end;