Thema: Delphi IP Adresse kontrollieren

Einzelnen Beitrag anzeigen

Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.771 Beiträge
 
Delphi 10.4 Sydney
 
#6

Re: IP Adresse kontrollieren

  Alt 19. Jan 2006, 19:18
mit dieser Funktion mache ich das immer:
Vielleicht ein wenig umständlich - aber es tut es.

Kann man in OnExit es Editfeldes einfügen.

Grüße
Klaus

Delphi-Quellcode:
FUNCTION check_ip_address(VAR ipaddress:String):Boolean;
VAR
  ip_address_ok: Boolean;
  dummy_text:String[3];
  dummy1_text,dummy_ip:String;

  FUNCTION check_segments:Boolean;
  VAR
    i,j:Byte;
  BEGIN
    j:=0;
    FOR i:=1 TO length(dummy_ip) DO
      IF dummy_ip[i] = '.THEN
        inc(j);
    IF j <> 3 THEN
      check_segments := false
    ELSE
      check_segments := true;
  END;

  FUNCTION check_double_dots:Boolean;
  VAR
    i:Byte;
  BEGIN
     check_double_dots := false;
     FOR i:=1 to length(dummy_ip)-1 DO
     BEGIN
       IF (dummy_ip[i] = '.') AND (dummy_ip[i+1] = '.') THEN
        check_double_dots := true;
     END;
  END;

begin

  ipaddress:=copy(ipaddress,1,pos(' ',ipaddress)-1);
  // check ip address
  dummy_ip:=ipaddress;

  ip_address_ok:=check_segments AND NOT check_double_dots AND (dummy_ip <> '0.0.0.0');

  IF ip_address_ok THEN
    BEGIN
      dummy_ip:='';
      dummy_text:= Copy(ipaddress,1,pos('.',ipaddress)-1);
      ip_address_ok:=false;
      IF (strToInt(dummy_text) >= 0) AND (strToInt(dummy_text) <=254) THEN
        BEGIN
          dummy_ip:=dummy_text+'.';
          dummy1_text := copy(ipaddress,pos('.',ipaddress)+1,length(ipaddress));
          dummy_text:= Copy(dummy1_text,1,pos('.',dummy1_text)-1);
          IF (strToInt(dummy_text) >= 0) AND (strToInt(dummy_text) <=254) THEN
            BEGIN
              dummy_ip:=dummy_ip+dummy_text+'.';
              dummy1_text := copy(dummy1_text,pos('.',dummy1_text)+1,length(dummy1_text));
              dummy_text:= Copy(dummy1_text,1,pos('.',dummy1_text)-1);
              IF (strToInt(dummy_text) >= 0) AND (strToInt(dummy_text) <=254) THEN
                BEGIN
                  dummy_ip:=dummy_ip+dummy_text+'.';
                  dummy1_text := copy(dummy1_text,pos('.',dummy1_text)+1,length(dummy1_text));

                  IF pos('.',dummy1_text) = 0 THEN
                     dummy_text:= dummy1_text
                  ELSE
                     dummy_text:= Copy(dummy1_text,1,pos('.',dummy1_text)-1);

                  IF dummy_text <> 'THEN
                    IF (strToInt(dummy_text) >= 0) AND (strToInt(dummy_text) <=254) THEN
                     BEGIN
                       ip_address_ok:=true;
                       dummy_ip:=dummy_ip+dummy_text;
                       ipaddress:=dummy_ip;
                     END;

                END;
            END;
        END;
    END;

    IF NOT ip_address_ok THEN
      StatusBar1.Panels[1].Text:= 'The ip address "'+ipaddress+'" has not the correct format!'
    ELSE
      StatusBar1.Panels[1].Text:= '';

  check_ip_address := ip_address_ok;

  END;
Klaus
  Mit Zitat antworten Zitat