I'm trying to implement validation for domain names (in e-mail addresses) and host names. In several other projects I found that for this purpose people use the same function. The question is, are there any possible differences between them in syntax and should I use separate validation routines for each of them or it's ok to keep only one?
The examples I found so far look like that (in pseudo-code):
str.split('.')
foreach part in str
if part.length > max
return "name is invalid"
if part contains invalid characters
return "name contains invalid characters"
return "name valid"
It is being used for both host names and domain names
One possible difference is because of context. In a context specifically expecting a hostname, an IP address is usually acceptable as well. This is especially true if you'll use the hostname to connect to it.
But if a domain name is expected, there's no such thing as "connecting" to a domain name, so an IP address would not be ok.