如何验证IP地址合法性?

xiaofeng_cxy 2002-07-15 04:28:05
请教各位高手!
在下在线恭候!
...全文
200 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
alaclp 2002-11-17
  • 打赏
  • 举报
回复
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;
cpls 2002-07-15
  • 打赏
  • 举报
回复
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;

Result:=True;
end;
micha_he 2002-07-15
  • 打赏
  • 举报
回复
gz
知足常乐 2002-07-15
  • 打赏
  • 举报
回复
a.b.c.d

让a,b,c,d都小于256就行了,其它的看你自己的网段配置
guguda 2002-07-15
  • 打赏
  • 举报
回复
gz

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧