Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript regular expression validation for domain name?

Tags:

javascript

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.

like image 925
gruppo Avatar asked Oct 23 '12 09:10

gruppo


4 Answers

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.

like image 183
gruppo Avatar answered Oct 20 '22 00:10

gruppo


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

like image 39
Bergi Avatar answered Oct 19 '22 22:10

Bergi


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>
like image 21
KS Rajput Avatar answered Oct 19 '22 22:10

KS Rajput


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
like image 45
L.Grillo Avatar answered Oct 19 '22 23:10

L.Grillo