How to validate a domain?

Red Squirrel

No Lifer
May 24, 2003
71,306
14,082
126
www.anyf.ca
What is the best way to programaticly validate a domain name?

At first I was thinking, easy, just need to make sure it's in format "word.ext"

Then I just make sure that word and ext don't use any special characters.

If it's www.word.ext then it is invalid because there's 3 sections, I only want 2. (don't care about subdomains in this particular case)

But here's my delima. There are domain TLDs like co.uk.

So how would I handle that? Do I need to tell my program what every single domain extension is? How do programs like cpanel do it?
 

Red Squirrel

No Lifer
May 24, 2003
71,306
14,082
126
www.anyf.ca
Hmm given that's from iana that's quite official right? Think I might go ahead and make my program use that. It will have it's own list but have an option to update with that list.
 

Red Squirrel

No Lifer
May 24, 2003
71,306
14,082
126
www.anyf.ca
Ran into an issue. That list only has the main TLDs but not the "Sub" TLDs like co.uk and stuff. So I don't know how to make my program know the difference between:

subdomain.domain.com

and

domain.co.uk

The 2nd one should either be rejected or truncated as I only want domain.com while the 2nd is valid because co.uk is actually a tld and not a domain.

This is a program where users will enter data, so I have to make it fool proof or it could render the entire hosting environment screwed up, like if someone decides to add co.uk as a domain then nobody will be able to add something.co.uk as the other person will own that domain. Or do I just let them put whatever they want and solve these issues as they come?

This is for a control panel, so it's equivalent to "addon domains" in cpanel. Anyone know how cpanel handles this? I don't have a cpanel server to test with.