11

How to check a valid domain name and username with regular expression in JavaScript?

function validate()
{
    var patt1=new RegExp(/^[a-zA-Z0-9._-]+\\[a-zA-Z0-9.-]$/);

    var text= document.getElementById('text1').value;

    alert(patt1.test(text));
}

But it does not work for me.

2
  • net.tutsplus.com/tutorials/other/… Commented Oct 23, 2012 at 9:57
  • 1
    What does not work? In which format do you want to accept them, why only alphanumerical characters? Commented Oct 23, 2012 at 9:58

5 Answers 5

17
function CheckIsValidDomain(domain) { 
    var re = new RegExp(/^((?:(?:(?:\w[\.\-\+]?)*)\w)+)((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.(\w{2,6})$/); 
    return domain.match(re);
} 

try this its work for me.

Sign up to request clarification or add additional context in comments.

2 Comments

This may work but it's confusing to pass a regexp literal to a RegExp constructor as @Bergi pointed out in their answer.
Not an answer. Test with "www.google.com", "google.com", "google.co.uk", and "www.google.co.uk". Validation also implies returning a boolean and this does not do that.
5

Don't mix up the RegExp constructor with regex literals. Use either

/^[a-zA-Z0-9._-]+\\[a-zA-Z0-9.-]$/

or

new RegExp("^[a-zA-Z0-9._-]+\\\\[a-zA-Z0-9.-]$");

Not sure what the backslash does in there, btw. Did you want to match a dot? In literal, use \., in string use \\..

Comments

5

Use this:

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

5 Comments

how about the 2 letter domains?
@Touchpad Try this: /^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/
@ZefirZdravkov this domain does not work: www.2test.com
@Touchpad ^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9](?:\.[a-zA-Z0-9]{2,})+$
that neither accounts for slash nor dash
1

check this: http://shauninman.com/archive/2006/05/08/validating_domain_names

/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i

1 Comment

2 letter domains?
-1

This code support sub-domains too:

^(([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]|[a-zA-Z0-9])\.)*[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$

1 Comment

late to the party but how about google.co.uk or hm.com ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.