How long can a TLD possibly be?

前端 未结 7 1941
忘了有多久
忘了有多久 2020-11-28 07:03

I\'m working on an email validation regex in PHP and I need to know how long the TLD could possibly be and still be valid. I did a few searches but couldn\'t find much infor

7条回答
  •  心在旅途
    2020-11-28 07:56

    Since I'm a .net developer following is the java-script representation of determining the longest TLD currently available.this will return the length of the longest TLD which you would be able to use in your RegEx.

    please try the following Code Snippet

    function getTLD() {
        var length = 0;
        var longest;
        var request = new XMLHttpRequest();
    
        request.open('GET', 'http://data.iana.org/TLD/tlds-alpha-by-domain.txt', true);
        request.send(null);
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                var type = request.getResponseHeader('Content-Type');
                if (type.indexOf("text") !== 1) {
                    var tldArr = request.responseText.split('\n'); 
                    tldArr.splice(0, 1);
    
                    for (var i = 0; i < tldArr.length; i++) {
                        if (tldArr[i].length > length) {
                            length = tldArr[i].length;
                            longest = tldArr[i];
                        }
                    } 
    
                    console.log("Longest >> " + longest + " >> " + length);
                    return length;
                }
            }
        }
    }

提交回复
热议问题