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

来源:百度知道 编辑:UC知道 时间:2024/06/30 17:20:32
Delphi如何判断IP地址是否正确,麻烦大家教教我该怎么写,谢谢!

uses winsock;

if inet_addr(pchar(edit1.Text))=INADDR_NONE then
showmessage('ip不正确 ');

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);