最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Regular Expression in JavaScript for valid Domain Name - Stack Overflow

programmeradmin4浏览0评论

I am having JavaScript as follows

function DomainValidation() {
        var val = document.getElementById('<%=txtDomainName.ClientID %>').value;
        if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.test(val)) {
            return true;
        }
        else {
            alert("Enter Valid Domain Name");
            return false;
        }
    }

for checking the valid domain name. It is working fine except when I try to enter the domain name with only two character it is not accepting. Since I have no idea of using Regular Expression, so wanna know where I should make change so that it accept domain name with only two characters also.

Note:- Before marking this a duplicate and showing me this I would like to inform that the above script is working fine but I want it to accept the domain name with only two characters too.

I am having JavaScript as follows

function DomainValidation() {
        var val = document.getElementById('<%=txtDomainName.ClientID %>').value;
        if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.test(val)) {
            return true;
        }
        else {
            alert("Enter Valid Domain Name");
            return false;
        }
    }

for checking the valid domain name. It is working fine except when I try to enter the domain name with only two character it is not accepting. Since I have no idea of using Regular Expression, so wanna know where I should make change so that it accept domain name with only two characters also.

Note:- Before marking this a duplicate and showing me this I would like to inform that the above script is working fine but I want it to accept the domain name with only two characters too.

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Dec 28, 2015 at 5:35 Naved AnsariNaved Ansari 6602 gold badges14 silver badges31 bronze badges 5
  • 1 Can you provide an example of a two-character domain name that you consider valid? – 200_success Commented Dec 28, 2015 at 5:38
  • Did you try entering the title of your question into a search engine? I did, and the first four hits came from this very site. – miken32 Commented Dec 28, 2015 at 5:39
  • Possible duplicate of Domain name validation with RegEx – miken32 Commented Dec 28, 2015 at 5:39
  • may be u guys dint read question carefully. I clearly mentioned that its working fine except for domain name with 2 character – Naved Ansari Commented Dec 28, 2015 at 5:41
  • @miken32 may be you should try adding domain name xy. using the script that is mentioned in your provided link – Naved Ansari Commented Dec 28, 2015 at 5:43
Add a ment  | 

2 Answers 2

Reset to default 5

Just change {1,61} to {0,61}, so that it won't except the middle character.

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

DEMO

Domain names can be tricky,

To support domain names like t.co you can use the below RegExp

^(?!-)[A-Za-z0-9-]+([-.]{1}[a-z0-9]+)*.[A-Za-z]{2,6}$

So you can pass the sample domain as first argument to the function below and it is expected to return true if the input is a valid domain and false otherwise.

const isValidDomainName = (supposedDomainName) => {
  return /^(?!-)[A-Za-z0-9-]+([\-\.]{1}[a-z0-9]+)*\.[A-Za-z]{2,6}$/i.test(
    supposedDomainName
  );
};

发布评论

评论列表(0)

  1. 暂无评论