0

I'm trying to create a domain name validation using jquery I want to allow all domain name inside that regex validation e.g.

www.abcd.xxx     valid 
www.abcd.xx.xx   valid  

What I'm trying to do is here is my code and regex :

$.validator.addMethod("customurl",
            function (value, element) {
                return /(?:https?:\/\/)?(?:www\.)?[a-z-]+\.(?:com|co.uk)(?:\.[a-z]{2,3})?/.test(value);
            },
            "Invalid url format"
            );

what above code will do is allow only .com and .co.uk domains but I want to allow for all it could be .org, .nl, .in, co.uk, .edu etc.

Can you guys please help me out with this.

Thanks Randheer

  • 1
    Possible duplicate of [Jquery how to validate domain name](http://stackoverflow.com/questions/7916370/jquery-how-to-validate-domain-name) – Anurag_Soni Nov 22 '16 at 12:35

1 Answers1

0

You Just need to update the regular expression:

Please try this It will help you :

$.validator.addMethod("customurl",
 function (value, element) {
 return /(?:https?:\/\/)?(?:www\.)?[a-z-]+\.(?:com|org|nl|in|edu|co\.uk)(?:\.[a-z]{2,3})?/.test(value);
            },
            "Invalid url format"
            );

Or You can Try This :

if(!/([a-z0-9-]+\.(?:com|net|org|co\.uk))(?:\/|$)/.test($("#domain").val())) {
        $('#error').html("Invalid domain name, , please correct before proceeding.");
        return false;   
}

And we can not put any values after . because then the domain will be invalid For example www.gmail.test.test is invalid So for that we have limited options And thats why we have mention all the known keys in it like .org,.net.in.edu.co etc

Anurag_Soni
  • 542
  • 2
  • 17