Wednesday, July 26, 2006

Validation of Host Name in Java .....

We needed to write a validate function for validating the host name. After reading RFC 1123 and RFC 952, here is what we required ....

1). It can contain only dots, dash and alphanumeric characters.
2). It cannot be more than 63 characters in length.
3). The first character must be an alphanumeric.
4). The last character cannot be a dot or dash.
5). There should atleast be one alphabet (This is for Linux).

This is the code we have written ......

public static boolean validateDomainName(String domainName) {
if((domainName == null) || (domainName.length()>63)) {
return false;
}
return domainName.matches(domainNameRule) && domainName.matches(oneAlpha);
}

where String domainNameRule = "("+domainIdentifier + ")((\\.)(" + domainIdentifier+ "))*";
String oneAlpha="(.)*((\\p{Alpha})|[-])(.)*";
String domainIdentifier="((\\p{Alnum})([-]|(\\p{Alnum}))*(\\p{Alnum}))|(\\p{Alnum})";

3 comments:

Anonymous said...

good help :)

Anonymous said...

soooooooooper

Nida Sahar said...

Why not use InetAddress Api's ?