Delphi如何判断IP地址是否正确

[复制链接]
查看11 | 回复0 | 2009-6-28 15:41:42 | 显示全部楼层 |阅读模式
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;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行