views:

81

answers:

2

Hi,

I want a solution to validate only domain names not full urls, The following example is what i'm looking for:

domain.com -> true
domain.net/org/biz... -> true
domain.co.uk -> true
sub.domain.com -> true
domain.com/folder -> false
domµ*$ain.com -> false

Thank you

+3  A: 

How about:

^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$
zildjohn01
Why the downvote? I tested it at http://regexpal.com/ and it matches all the OP's test data.
zildjohn01
What downvote?` `
Josh K
+1 from me, but {2,6}? What TLDs are > 4?
Lauri Lehtinen
Whoever downvoted took it back. @Lauri `.museum` and `.travel`.
zildjohn01
@zildjohn01 Learned something new today as well ;-) Thanks
Lauri Lehtinen
Thanks man , exactely what i'm looking for :D
David
+1 from me. And for the record, depending on what is what you want to do with that regexp, keep in mind that example.{com,net,org} are reserved and can't be registered (cf. http://en.wikipedia.org/wiki/Example.com).
matias
A: 

Remember, regexes can only check to see if something is well formed. "www.idonotexistbecauseiammadeuponthespot.com" is well-formed, but doesn't actually exist... at the time of writing. ;) Furthermore, certain free web hosting providers (like Tripod) allow underscores in subdomains. This is clearly a violation of the RFCs, yet it sometimes works.

Do you want to check if the domain exists? Try dns_get_record instead of (just) a regex.

Charles