20

This is my code for validating domain name.

function frmValidate() {
    var val = document.frmDomin;
    if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/.test(val.name.value)) {
    }
    else {
        alert("Enter Valid Domain Name");
        val.name.focus();
        return false;
    }
}

and

<form name="frmDomin" action="" method="post" onsubmit="return frmValidate();">
Domain Name : <input type="text" value="" id="name" name="name"  />
</form>

Now I entered http://devp1.tech.in and it alert the message. I want to enter sub domain also. How to change this? I should not get alert.

Denis L
  • 3,209
  • 1
  • 25
  • 37
PNG
  • 267
  • 1
  • 4
  • 9

5 Answers5

44

Try this:

^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$

Demo

Alex Gyoshev
  • 11,929
  • 4
  • 44
  • 74
walid toumi
  • 2,172
  • 1
  • 13
  • 10
  • Can you update this answer to also allow subdomains ? – Héctor León Jun 20 '16 at 14:03
  • 3
    Something missing here, It doesn't match `www.test2.com` but it does match `test2.com`. – yeya Nov 05 '16 at 20:40
  • it does not test for domain in numbers like 192.168.3.1 – test_124 Apr 17 '18 at 07:58
  • 1
    In my implementation of this answer, it worked as `/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+/` regex – Abel Callejo Dec 26 '18 at 15:56
  • Why aren't underscores included in regex? – Miguel Mota Jan 28 '20 at 22:45
  • @MiguelMota Underscore is no allowed characters in domain names. – Mecki Nov 12 '21 at 14:43
  • 1
    This regex does not work correctly. It should match all samples given here except the last one which is no valid domain: https://regex101.com/r/Vi2CYC/1 But it fails to match 3 valid domains and does match the invalid one at the end. For comparison, this one works as expected: https://regex101.com/r/d7YuFt/1 – Mecki Nov 12 '21 at 22:13
  • 1
    This also fails for single-letter domains, i.e. `a.co`, but those are valid. – J23 May 25 '22 at 21:36
13

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)

Lux
  • 1,540
  • 1
  • 22
  • 28
11

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. ...

Nick Winnubst
  • 111
  • 1
  • 2
1
<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.

Sodium
  • 1,016
  • 1
  • 9
  • 22
0

Try this regex:

/([a-z0-9]+\.)*[a-z0-9]+\.[a-z]+/
Mogli
  • 27
  • 3