-
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.
-
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.
>
-
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;
}
-
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
-
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
>
>
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks