I know this is an old question, but it came up high on google when searching for TLD lists so I figured I would post an updated answer.
Like @BillThor, I'm also a fan of using DNS check (eg is there an MX record for name email.split('@')[1]
when validating email.
The goal isn't to force user to enter a real email address...in that case you would usually send a verification email. Rather you are trying to help the user in case there is a typo/etc....do as much verification as possible to make the user's life easier. Any user that doesn't want to give out their real email has many free "disposable email" services available to avoid this.
For general domain validation (as opposed to email validation) when checking TLDs it is important to remember that some TLDs have different rules. This is where the iana list isn't ideal. For instance domain.co.uk is a valid domain. domain.uk is not. uk
is on the iana list, co.uk
is not.
Update: As if this year .uk is now allowed in addition to .co.uk...but
I'll leave it as an example.
This is because each TLD is allocated by iana to a specific "registry" that is responsible for administrating that TLD and they can create their own rules (e.g. the .co.uk example).
The best source I have found for validating TLDs is here: https://publicsuffix.org/ You can quite easily script a daily/weekly/monthly update and then run your code against the list. This list includes public and private TLDs and some basic corresponding rules for each TLD.
For more context you can also see this outdated mozilla wiki page: https://wiki.mozilla.org/TLD_List
Of course, just validating the TLD doesn't mean that the domain is valid or registered or the correct input...but the original question is about determining if a domain has a valid TLD.
aside... should you validate/verify emails?
If you collect emails from the user (e.g. required field in your form) then you really really really should be both validating AND verifying the email belongs to the user (e.g. by sending an email with a short code for the user to enter into your site).
- validating that it looks/smells like a correct email address (e.g. is the tld/domain valid and has an MX record). this gives instant feedback to your users if they have a typo in their form.
- verifying that email if you intend to store it and use it for logins, marketing, etc.
Email verification is something that many companies don't bother to do. My "honey pot" gmail addresses get huge amounts of marketing and transactional email in a variety of languages from major companies where the user gave them my email address by mistake and the company did not both to verify it. Many of these transactional emails do not include a "this is not me, please delete my email address" link...they say they have a right to email you transactional emails due to your relationship with them (none...they were given the wrong address and didn't verify).
This is not only legally problematic in some jurisdictions, but can be a huge risk to the privacy of their customers. And of course super annoying to me as the recipient. Here are some real examples of what happens when you don't verify emails (a few of many off the top of my head)...
- honda of america. someone bought a new honda pilot and gave my email address. I get invites from honda to login to their hondacare account to manage online services, enable honda app for unlocking the vehicle, etc.
- macys. order confirmations including direct (no-login) links to manage and cancel orders. Includes name, address, phone and partial credit card details.
- Stanbic Bank Tanzania. transaction notifications, balance notifications, personal contact information.
- SNCF (french railway system) - I get emails allowing me to see, edit, cancel tickets for travelers.
- tons of marketing I didn't sign up for.
No easy way to get my data deleted without contacting every company's support team, figuring out how to talk to a real person and explaining in length that I am not a customer and they need to remove my email from their customer database. I don't bother anymore. The support people MIGHT delete my email but they don't fix the problem to prevent it from happening to others.
I'm a good Samaritan, but this is a goldmine for id thieves who can signup many common "typo" email addresses with gmail and wait for the data leakage from companies that don't verify emails.
Don't be one of those companies. ALWAYS verify your email addresses. If you can't verify them within a certain short time period (few days), then delete them from your database and don't use them (ie no more email beyond the verification email). If you aren't willing to verify email addresses...don't collect them in the first place.