• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1239
  • Last Modified:

data validation for a host name in Java

Can you provide code for validating a host name such as ...com, edu... in java, not java script? I have the following code

 String MyIPvalue = request.getParameter("IPvalue");
                   

                    if (  MyIPvalue.matches( "^.[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}" ) == false )
                {
                 ............................
                }      
                  else if (  MyIPvalue is a host name..... ==false)
                {
                      ..............................
                }
               
0
wasabi3689
Asked:
wasabi3689
1 Solution
 
CEHJCommented:
Isn't it more important to check if the host is resolvable?
0
 
wasabi3689Author Commented:
what does it mean?
0
 
CEHJCommented:
That it's a real host
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
wasabi3689Author Commented:
it doesn't matter. what I need is the input is a hostname format.
0
 
CEHJCommented:
zzzz.nnn would probably match any pattern you can find, despite the fact it's nonsense. Anything more restricive would have to cope with any and all TLDs, and that would soon fall out of date. That's why it's better to resolve the host, despite the fact it's slower
0
 
objectsCommented:
a general regex would be something like:

[a-z]([a-z0-9-]*[a-z0-9])?(\.[a-z]([a-z0-9-]*[a-z0-9])?)*

> it doesn't matter. what I need is the input is a hostname format.

and just because its resolvable doesn't mean its a real host anyway
0
 
TheMajesticCommented:
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+ "))*";


this code contains
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).

String oneAlpha="(.)*((\\p{Alpha})|[-])(.)*";
String domainIdentifier="((\\p{Alnum})([-]|(\\p{Alnum}))*(\\p{Alnum}))|(\\p{Alnum})";
0
 
CEHJCommented:
The above returns false for microsoft.com
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now