0

I need a regular expression which will check the domain name including characters like a-z A-Z 0-9 -

Also does any domain name having underscore _ character?

Please help me.

4
  • 1
    Did you try filter_var() with FILTER_VALIDATE_URL? Commented Jun 4, 2012 at 7:24
  • no I did not. But working on one scenario where we should not allow domain name like www.platform.com/test. /test or ?test should not be allowed at all. Commented Jun 4, 2012 at 7:32
  • I meant to say there should be only domain name or domain name with subdomain. No other parts are allowed. Commented Jun 4, 2012 at 7:34
  • FILTER_VALIDATE_URL does NOT do what you are asking for. Commented Dec 2, 2012 at 9:41

2 Answers 2

1

I guess this is what youre searching for:

^[a-zA-Z0-9]+([a-zA-Z0-9-.]+)?.(com|org|net|mil|edu|de|COM|ORG|NET|MIL|EDU|DE)$

Sign up to request clarification or add additional context in comments.

5 Comments

I guess it will not work as extension could be anything like .in or .co.in. I just wanted to make sure there should not be any slashes or any unwanted character. Only domain name should be there.
there are about 250 other top level domains
and you can add i modifier to your regex to prevent duplication!
Even more than that, with the advent of .companyname and similar TLDs. Having a whitelist of acceptable TLDs is never gonna work...
@UmeshKulkarni: I feel using the above answers as a base, you should be able to work this up.
1

This regular expression would work on any number of subdomains and all top level domains (the world is not just murka):

/^ (?: [a-z0-9] (?:[a-z0-9\-]* [a-z0-9])? \. )*  #Subdomains
   [a-z0-9] (?:[a-z0-9\-]* [a-z0-9])?            #Domain
   \. [a-z]{2,6} $                               #Top-level domain
/ix

Domain names cannot have underscores. Only [a-zA-Z0-9], as well as hyphens if they are not at the beginning or the end of a subdomain name.

2 Comments

DOMAIN names CAN have underscores. HOST NAMES can't. There is a difference and he is most likely worried about host names but I just thought I should point this out.
What DOMAIN name can have underscore? I tried this..I went to godaddy, tried searching for e_s.com and it corrected it to es.com. Then, went into cPanel to try to add a subdomain with _, and it said it was invalid. Trying to figure out what DOMAIN name can have underscore? Are you referring to what comes after the /? Just trying to figure this out. Sorry to bring this back up. This post was helpful to me, so wanted to double check what you were referring to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.