06-20-2011، 01:31 PM
A simple way to check whether an IP address is valid.
در سورس بالا که با دلفی نوشته است میتونید چک کنید ایا ای پی ولید هست یا نه
کد:
function IsIPAddress(zvIPAddressID: string): boolean;
var
ipLengthOfGroup, ipPosition, ipNoOfGroups: integer;
ipGroupNum, Code: integer;
begin
IsIpAddress := False;
ipNoOfGroups := 0;
ipLengthOfGroup := 0;
for ipPosition := 1 to Length(zvIPAddressID) do
case Ord(zvIPAddressID[ipPosition]) of
48..57:
begin
inc(ipLengthOfGroup);
if (ipLengthOfGroup > 3) then exit;
end;
46:
begin
inc(ipNoOfGroups);
Val(Copy(zvIPAddressID, ipPosition - ipLengthOfGroup, ipLengthOfGroup), ipGroupNum, Code);
if ((ipNoOfGroups > 3) or (ipLengthOfGroup = 0)) or (ipGroupNum > 255) then exit;
ipLengthOfGroup := 0;
end;
else
exit;
end;
Val(Copy(zvIPAddressID, ipPosition - ipLengthOfGroup, ipLengthOfGroup), ipGroupNum, Code);
IsIPAddress := (ipNoOfGroups = 3) and (ipLengthOfGroup > 0) and (ipGroupNum < 256);
end;