不用第三方控件, 怎样用最简单的方式格式化IP地址并判断是否正确?用maskedit编辑的话,不知道127.0.0.1这种格式怎么配置
三位三位的话,192.168.18.118跟192.168.018.118这两个ip地址是不同的

解决方案 »

  1.   


    找 IPEdit 撒。www.2ccc.com 有下载。
      

  2.   

    我写了个函数给你:
    function IPVertify(sIP: String): Boolean;
    Var
      iCount,iPos:Word;
      sTmp:String;
    begin
      Result:=True;
      sIp:=Trim(sIP);
      
      For iCount:=1 To 3 Do
      Begin
        iPos:=Pos('.',sIP);
        IF iPos=0 Then
        Begin
          Result:=False;
          Break;
        End;
        
        sTmp:=Copy(sIP,1,iPos-1);
        Try
          IF StrToInt(sTmp)>225 Then
          Begin
            Result:=False;
            Break;
          End;
        Except
          Result:=False;
          Break;
        End;
          
        Delete(sIp,1,iPos-1);
        IF Copy(sIp,1,1)<>'.' Then
        Begin
          Result:=False;
          Break;
        end;
        Delete(sIP,1,1);
      End;  If Result Then
      Try
         IF StrToInt(sIP)>225 Then
         Begin
          Result:=False;
          Exit;
         End;
      Except
         Result:=False;
      End;
    end;
      

  3.   

    看看我常用的
    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
            Result:=false;
            exit;
          end;
          ips[i]:=ips[i]+ip[j]
        end
        else inc(i);  if (i<>4)
          or((strtoint(ips[1])>255)or(strtoint(ips[1])<0))
          or((strtoint(ips[2])>255)or(strtoint(ips[2])<0))
          or((strtoint(ips[3])>255)or(strtoint(ips[3])<0))
          or((strtoint(ips[4])>255)or(strtoint(ips[4])<0))
      then Result:= false else Result:= true;end;