Validating domain names without regular expressions is not recommended due to potential security vulnerabilities and inconsistent results. Regular expressions provide a robust and standardized approach to ensure accuracy.
To validate domain names using regular expressions, consider the following pattern:
^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i
This expression comprises multiple criteria:
An improved PHP function for domain name validation:
<code class="php">function is_valid_domain_name($domain_name) { return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name) && preg_match("/^.{1,253}$/", $domain_name) && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name)); }</code>
Domain Name | Validation Result |
---|---|
a | Yes |
0 | Yes |
a.b | Yes |
localhost | Yes |
google.com | Yes |
news.google.co.uk | Yes |
xn--fsqu00a.xn--0zwm56d | Yes |
goo gle.com | No |
google..com | No |
google.com | No |
google-.com | No |
.google.com | No |
Popular Tutorials
More>
Latest Downloads
More>
|