方法一:
uses WinSock
function IsLegalIP(IP:string):boolean;
begin
if Longword(inet_addr(pchar(IP)))=INADDR_NONE then
begin
result:=false;
exit;
end
else result:=true;
end;
方法二:
function IsLegalIP(IP:string):boolean;
var
i,j,l:integer; ips:array [1..4] of string;
begin
i:=1;
for l:=1 to 4 do ips[l]:='';
for j:=1 to length(ip) do
if ip[j]<>'.' then
begin
if (ip[j]<'0')or(ip[j]>'9') then
begin
//showmessage(ip[j]);
Result:=false;
exit;
end;
ips:=ips+ip[j]
end
else inc(i);
if (i<>4)
or((strtoint(ips[1])>255)or(strtoint(ips[1])<1))
or((strtoint(ips[2])>255)or(strtoint(ips[2])<1))
or((strtoint(ips[3])>255)or(strtoint(ips[3])<1))
or((strtoint(ips[4])>255)or(strtoint(ips[4])<1))
then
Result:= false
else
Result:= true;