Validating an IP address


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Validating an IP address

  1. #1
    Kathy Guest

    Validating an IP address


    Hi,

    I'm looking for an algorithm to take a user inputted string of dotted decimal
    notation and validate that it's the right format for an IP address.

    This will be used in an applet so I can't use the InetAddress class. I don't
    need to do a host lookup anyway, just check for a valid string and store
    it away.

    Any help would be appreciated,

    Kathy.


  2. #2
    John MacLatchie Guest

    Re: Validating an IP address


    Hi Kathy,
    I wasn't sure how much vetting you needed but here's a small snippet of code
    which will return "true if the input string contains only numeric or dot
    chatacters and false otherwise. Segment range checking could be easily added
    as could default values if necessary.
    ...John

    Boolean validateIP(String ip)
    {
    //-->
    // Simply validate the string for dots and all numerics
    //-->
    char[] ca = ip.toCharArray();
    for (int i = 0; i < ca.length; i++)
    {
    if (!Character.isDigit(ca[i]))
    {
    if (ca[i] != '.')
    {
    return false; // ip contains non-numeric/non-dot char
    }
    }
    }
    return true; // ip contains all numeric and dot chars
    }


    "Kathy" <kathryn.nelson@usa.net> wrote:
    >
    >Hi,
    >
    >I'm looking for an algorithm to take a user inputted string of dotted decimal
    >notation and validate that it's the right format for an IP address.
    >
    >This will be used in an applet so I can't use the InetAddress class. I don't
    >need to do a host lookup anyway, just check for a valid string and store
    >it away.
    >
    >Any help would be appreciated,
    >
    >Kathy.
    >



  3. #3
    John jmaclatchie Guest

    Re: Validating an IP address


    Well your project dovetailed into one I'd been working on so I decided to
    take the sample a bit further to make it functional for my own use. There
    are probably many ways to do this. Below is my result, I left the debugging
    messages in so you could see what was happening.
    ...John

    //-->
    // Method takes a string in format of n.n.n.n, validates as follows
    // a) contains four "dot" deliminated strings
    // b) each string is no greater than 3 characters and
    // contains only numeric characters
    // c) each numeric value does not exceed 255, presently the largest
    // address component value.
    //-->
    Boolean validateIP(String ip)
    {
    //String ip = new String("123.255.230.1"); // testing
    int i = 0;
    int j = 0;
    java.util.StringTokenizer parser = new java.util.StringTokenizer(ip,
    ".");
    java.util.Vector vParts = new java.util.Vector(4);
    String aPart = null;
    char[] ca;
    // First tokenize the string with delm = .
    while(parser.hasMoreTokens())
    {
    aPart = parser.nextToken();
    vParts.addElement(aPart);
    }
    // vet for 4 parts
    if (vParts.size() != 4)
    {
    System.out.println("Adddress string" + ip + " is either to short or
    to long");
    return false;
    }
    // vet each part
    for (i = 0; i < 4; i++)
    {
    aPart = (String)vParts.elementAt(i); // get the segment
    ca = aPart.toCharArray();
    if (aPart.length() > 3) // to long
    {
    System.out.println("Segment " + aPart + " in string " + ip + " has
    length > 3 digits");
    return false;
    }
    for (j = 0; j < ca.length; j++)
    {
    if (!Character.isDigit(ca[j])) //non-numeric
    {
    System.out.println("Segment " + aPart + " in string " + ip + " contained
    non numeric character");
    return false;
    }
    }
    if (Integer.valueOf(aPart).intValue() > 255) // number to big
    {
    System.out.println("Component " + aPart + " in string " + ip + " has
    value > 255");
    return false;
    }
    }
    System.out.println(ip + " is a valid IP Address String");
    return true;
    }


  4. #4
    Nikhil Guest

    Re: Validating an IP address


    Hi Guys,
    Here is very small code to check the valid IP address

    /////////////////
    public static boolean isValidIPAddr(String ipaddr)
    {
    StringTokenizer st = new StringTokenizer(ipaddr, ".");
    int tmpval, count = 0;
    while (st.hasMoreTokens())
    {
    try
    {
    tmpval = Integer.parseInt(st.nextToken());
    if (tmpval>255)
    return false;
    count++;
    }
    catch(NumberFormatException e)
    {
    return false;
    }
    }
    if (count == 4)
    return true;
    else
    return false;
    }
    //////////////////////


    Cheers,
    Nikhil



  5. #5
    Nikhil Guest

    Re: Validating an IP address


    Hi,
    I have forgotten to check the negative value
    use
    if ( (tmpval>255) || (tmpval < 0) )
    instead of
    if (tmpval>255) please ..

    Cheers,
    Nikhil

    "Nikhil" <mamo_nil@hotmail.com> wrote:
    >
    >Hi Guys,
    > Here is very small code to check the valid IP address
    >
    >/////////////////
    >public static boolean isValidIPAddr(String ipaddr)
    >{
    > StringTokenizer st = new StringTokenizer(ipaddr, ".");
    > int tmpval, count = 0;
    > while (st.hasMoreTokens())
    > {
    > try
    > {
    > tmpval = Integer.parseInt(st.nextToken());
    > if (tmpval>255)
    > return false;
    > count++;
    > }
    > catch(NumberFormatException e)
    > {
    > return false;
    > }
    > }
    > if (count == 4)
    > return true;
    > else
    > return false;
    >}
    >//////////////////////
    >
    >
    >Cheers,
    >Nikhil
    >
    >



  6. #6
    Kathy Guest

    Re: Validating an IP address


    Thanks for the code.
    I'll try it out!

    Kathy.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center