Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Check Valid IP Address

Title: Check Valid IP Address Question: A simple way to check whether an IP address is valid. Answer: 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 end;