Checking the correct javascript expression for a domain name?

how to check the correct domain name and username with regular expression in javascript?

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

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

try this job for me.

+3
source

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
source

Do not mix RegExp constructor with regular expressions . Use

 /^[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, by the way. Do you want to combine the point? In the literature, use \. , in a line use \\. .

+2
source

check this out: 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
source

This may be a little off topic, but after searching the Internet for several hours and testing various RegExp (which will work for me). I decided to write my own function, which would take a list of valid TLD extensions and check against them. So for those who have a similar problem, check out my Javascript function:

 function domainCheck(dom) { // convert input to lowercase. dom = dom.toLowerCase(); // find the first occurance of '.' pos = dom.indexOf("."); // Using the first occurance of '.' // find the extension submitted. tld = dom.substring(pos); switch(tld) { // TLD to accept. case '.com': return true; break; case '.co.uk': return true; break; case '.eu': return true; break; case '.io': return true; break; case '.co': return true; break; case '.net': return true; break; default: return false; } } 

I created a demo using this function here: http://jsfiddle.net/netfox/MhPG8/19/embedded/result/

0
source

This code also supports subdomains:

 ^(([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
source

All Articles