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 |
Latest Articles by Author
Latest Issues
How to display the mobile version of Google Chrome
Hello teacher, how can I change Google Chrome into a mobile version?
From 2024-04-23 00:22:19
0
11
2430
There is no output in the parent window
document.onclick = function(){ window.opener.document.write('I am the output of the child ...
From 2024-04-18 23:52:34
0
1
1928
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|