Home » Developer & Programmer » Forms » IP Address Validation
IP Address Validation [message #220056] Sun, 18 February 2007 04:27 Go to next message
ataufique
Messages: 79
Registered: November 2006
Member
Has any one written the validation code for
IP Address...
i.e it should be any thing from 0.0.0.0 to 255.255.255.255

pls pass the code...

Thanks in advance..
Re: IP Address Validation [message #220060 is a reply to message #220056] Sun, 18 February 2007 06:25 Go to previous messageGo to next message
Frank
Messages: 7877
Registered: March 2000
Senior Member
According to Google, about 258000 people asked this before you...
And look which site's on top!
Re: IP Address Validation [message #220108 is a reply to message #220060] Sun, 18 February 2007 21:24 Go to previous messageGo to next message
ataufique
Messages: 79
Registered: November 2006
Member
..The first site was a java script based check...
I dont know how to use that in my Forms 10g.
IF anyone has any idea....pls help
Re: IP Address Validation [message #220127 is a reply to message #220108] Mon, 19 February 2007 00:45 Go to previous messageGo to next message
Frank
Messages: 7877
Registered: March 2000
Senior Member
I got an orafaq page as first hit...
Second hit was Oracle Techforum, but I'm sure you saw that. I am sure you didn't stop after the first hit on Google..
Re: IP Address Validation [message #390636 is a reply to message #220056] Mon, 09 March 2009 01:48 Go to previous messageGo to next message
harshans
Messages: 1
Registered: March 2009
Junior Member
CREATE OR REPLACE FUNCTION isvalidip (ipaddr VARCHAR2)
   RETURN NUMBER
IS
   tmpvar                 NUMBER;
   v_IPaddr VARCHAR2(64);
   v_validip              INT;
   v_pos1                 NUMBER;
   v_pos2                 NUMBER;
   v_pos3                 NUMBER;
   v_pos4                 NUMBER;
   v_len                  NUMBER;
   v_found_invalid_char   NUMBER;

BEGIN
   tmpvar := 0;
   v_IPaddr := ipaddr;

   -- If the IP address is NULL, check if the node name contains a valid IP address
   IF (v_ipaddr IS NULL OR v_ipaddr = '' OR v_ipaddr = ' ')
   THEN
      RETURN 0;
   END IF;

   -- Check if IP address is valid, if not set IP address to NULL.
   v_validip := 0;
   -- Trim leading and trailing spaces, if any
   v_ipaddr := RTRIM (LTRIM (v_ipaddr));
   -- Make sure there are no invalid characters
   v_found_invalid_char := 0;

   FOR i IN 1 .. LENGTH (v_ipaddr)
   LOOP
      IF (INSTR ('.0123456789', SUBSTR (v_ipaddr, i, 1)) = 0)
      THEN
         v_found_invalid_char := 1;
         EXIT;
      END IF;
   END LOOP;

   IF (v_found_invalid_char = 0)
   THEN
      -- Get dot positions
      v_pos1 := INSTR (v_ipaddr, '.');
      v_len := LENGTH (v_ipaddr);
      v_pos2 := v_pos1 + INSTR (SUBSTR (v_ipaddr, - (v_len - v_pos1)), '.');
      v_pos3 := v_pos2 + INSTR (SUBSTR (v_ipaddr, - (v_len - v_pos2)), '.');
      -- Check for extra dot positions
      v_pos4 := v_pos3 + INSTR (SUBSTR (v_ipaddr, - (v_len - v_pos3)), '.');

      -- Check for at least one character and a value less than 255
      IF (    v_pos1 > 1
          AND v_pos2 - v_pos1 > 1
          AND v_pos3 - v_pos2 > 1
          AND v_len - v_pos3 > 0
          AND v_pos4 = v_pos3
         )
      THEN
         IF     (TO_NUMBER (SUBSTR (v_ipaddr, 1, v_pos1 - 1)) <= 255)
            AND (TO_NUMBER (SUBSTR (v_ipaddr, v_pos1 + 1, v_pos2 - v_pos1 - 1)) <=
                                                                           255
                )
            AND (TO_NUMBER (SUBSTR (v_ipaddr, v_pos2 + 1, v_pos3 - v_pos2 - 1)) <=
                                                                           255
                )
            AND (TO_NUMBER (SUBSTR (v_ipaddr, v_pos3 + 1, v_len - v_pos3)) <=
                                                                           255
                )
         THEN
            v_validip := 1;
         END IF;
      END IF;
   END IF;

   /* If IP address in not valid, set it to NULL */
   IF v_validip <> 0
   THEN
      RETURN 1;
      v_ipaddr := NULL;
   END IF;

   RETURN 0;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      NULL;
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END isvalidip;
/

[EDITED by LF: applied [code] tags]

[Updated on: Mon, 09 March 2009 02:16] by Moderator

Report message to a moderator

Re: IP Address Validation [message #567869 is a reply to message #390636] Sun, 07 October 2012 17:16 Go to previous messageGo to next message
gold36
Messages: 1
Registered: October 2012
Junior Member
@harshans @ataufique thanks.
how about handling multiple ip addresses separated by comma. for eg: 12.222.35.42,36.32.34.55,...

Re: IP Address Validation [message #567909 is a reply to message #567869] Mon, 08 October 2012 05:00 Go to previous message
Littlefoot
Messages: 19694
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Split and check them one by one.
The first part (splitting) has been described quite a few times in our SQL & PL/SQL forum (for example, here. Search for more examples.). The second part (validation) is described above.
Previous Topic: windows picture and fax viewer
Next Topic: Format mask allows to click anywhere on the field
Goto Forum:
  


Current Time: Thu Oct 23 15:55:10 CDT 2014

Total time taken to generate the page: 0.13090 seconds