static boolean validate(String ipv4_address) {
int seperators = 0;
int digits = 0;
int partval = 0;
if (ipv4_address.length() > 16) return false; // length condition satisfied
else {
for (int i = 0; i < ipv4_address.length(); i++) {
if (ipv4_address.charAt(i) == '.') {
seperators++;
digits = 0;
partval = 0;
if (seperators >= 4)
return false; // checked the no of . 's in the address
} else {
if (!Character.isDigit(ipv4_address.charAt(i))) {
return false;
}
partval = partval * 10 + Integer.parseInt(Character.toString(ipv4_address.charAt(i)));
if (++digits > 3)
return false;
if (digits == 3) {
if (partval > 255) return false;
}
}
}
}
return true;
}
Saturday, April 9, 2011
validate a ip address in java (ipv4)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment