How to validate domain name using regex?
Try this:
^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$
Demo
I'd advise reading up on O'Reilly's guide: https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch08s15.html
Try using https://regex101.com/ to validate the results of your regex.
8.15. Validating Domain Names Problem You want to check whether a string looks like it may be a valid, fully qualified domain name, or find such domain names in longer text.
Solution Check whether a string looks like a valid domain name:
^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$
Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python
\A([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\Z
Regex options: Case insensitive Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby Find valid domain names in longer text:
\b([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\b
Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby Check whether each part of the domain is not longer than 63 characters:
\b((?=[a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b
Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby Allow internationalized domain names using the punycode notation:
\b((xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\b
Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby Check whether each part of the domain is not longer than 63 characters, and allow internationalized domain names using the punycode notation:
\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b
Regex options: Case insensitive Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby
Discussion A domain name has the form of domain.tld, or subdomain.domain.tld, or any number of additional subdomains. ...
This is a little on the heavy side:
^(?:(?:(?:[a-zA-z\-]+)\:\/{1,3})?(?:[a-zA-Z0-9])(?:[a-zA-Z0-9\-\.]){1,61}(?:\.[a-zA-Z]{2,})+|\[(?:(?:(?:[a-fA-F0-9]){1,4})(?::(?:[a-fA-F0-9]){1,4}){7}|::1|::)\]|(?:(?:[0-9]{1,3})(?:\.[0-9]{1,3}){3}))(?:\:[0-9]{1,5})?$
Will match:
google.com
db.my-website.co.us
ftp://container-617.databases.online
many-ports.com:7777
Note: will not match localhost
IPv4
192.168.3.1
127.0.0.1:3306
IPv6 (partial support)
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
[2001:db8:85a3:0:0:8a2e:370:7334]
(same as previous)[da7a:ba5e:da7a:ba5e:da7a:ba5e:da7a:ba5e]:3306
[::1]
(localhost loopback)[::]
(unspecified address)
But not (IPv6)
- [
2001:db8:85a3::8a2e:370:7334]
This regular expression does not support collapsing consecutive 0-segments into a '::' in IPv6 addresses. (read: don't try this on IPv6 addresses)
<script>
function frmValidate() {
var val = document.frmDomin.name.value;
if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.test(val)) {
alert("Valid Domain Name");
return true;
} else {
alert("Enter Valid Domain Name");
val.name.focus();
return false;
}
}
</script>
Note : This will not validate Url.