Email addresses are hard to validate. You can’t just look at an email address and say “this exists” or “this doesn’t exist.” There are a number of reasons why this is the case:
The most obvious reason is that there are many different formats for email addresses. For example, someone could have an address like joe@joesdomain.com, joe@joe’s domain dot com, or even joe@joe’s domain dot com slash slash slash slash slash slash JoesDomain (with a lot of underscores).
The second reason has to do with how people enter their email addresses when they’re not actually typing them into an email client application—instead, they’re filling out forms somewhere else, like on your website or in an app they downloaded from the App Store. People often make typos when typing in their email address manually (like using Johne instead of john), but they probably make even more mistakes when entering it via automated form-filling software!
Use a regular expression pattern to validate email addresses
- Use a regular expression pattern to validate email addresses. Regular expressions are a powerful tool for validating email addresses, but they’re not perfect. You can’t use them to verify that an address is invalid or expired without first checking its validity with the other techniques we’ve discussed above.
- Regular expressions are easy to learn and use. The syntax of regular expressions is well-documented online, and it’s not difficult to find examples of how you might use them in your code base or website as you’re writing code (we’ll cover this in more detail later).
Don’t use regular expressions to validate domain names; this is error-prone
It’s not uncommon to see regular expressions used to validate domain names. These are usually implemented by adding the domain name as a string to the regex and then calling matches() on it. For example:
>>> from re import compile
>>> compile(r'[a-zA-Z0-9\.\-]{4,}?$’,reversed =True)
([‘[a-zA-Z0-9\\.-]+’], r, s, m)
>>> regexes = […](compile(r'[a-zA-Z0-9\.\-]+$’), …) …)
This will work fine for most cases where you’re validating emails or social media handles or phone numbers, but when it comes time to validate an address like “1 user@example.com”, this approach fails miserably because there’s no way for a regular expression engine like Python’s re module to know whether you mean user@example.com or 1user@example.com (or even 1user@examplecoom).
Don’t try to write your own regular expressions, there are lots of good ones available.
There are lots of good regular expression libraries in most programming languages. Python has two built-in ones, re and regex, Perl has Perl, PHP has preg_match, and JavaScript has RegExp. The Java language comes with the java.util.regex package that lets you use regular expressions to search text or parse HTML documents.
You may need to make a few compromises with regard to which email addresses you deem valid or invalid. In particular, allow for some fairly common typos and mistakes that people make when entering their email address.
One thing you may need to do—and this is optional, so don’t feel like you have to do it if you don’t want to—is make a few compromises with regard to which email addresses you deem valid or invalid. In particular, allow for some fairly common typos and mistakes that people make when entering their email address.
For example:
- “joe@examplecom” should be allowed as a valid address even though it’s missing the period after “example.” This is because many people will omit the period when entering their own name in an email field, so it’s reasonable for us not to expect that every single person will enter their name exactly as it appears on their passport/driver’s license/etc., especially considering how long these things are (and how difficult they are to type).
- “joe@exmplecom” should also be allowed as a valid address because most people won’t notice they’re typing an extra word by accident, especially if they’re doing something else at the same time (e.g., watching TV while they type). If someone enters this same typo three times in a row accidentally, then we can probably assume that they just want all four words there rather than assuming any malice on his part.
Implement an AJAX-style validation system in the front end so you can give the user immediate feedback on whether the email address they entered is valid or not.
To validate an email address in the front end, you’ll need to use JavaScript. If your project is written with jQuery, there are a number of plugins you can use. If not, feel free to use whatever framework you prefer (Angular or React).
In addition to using a JavaScript library or framework, it’s also possible for you to implement this validation yourself using the browser’s built-in validation. This process involves creating a regular expression that matches the format of an email address and then checking whether each value passed into its function matches against it. You can then return true or false based on whether they match.
There are also services available online that provide this service for free such as emailvalidation.io.
Combine the frontend system with a backend validation system that makes sure that the email domain actually exists (using DNS).
You can also use a DNS lookup to validate the domain. A DNS query checks whether or not the domain exists. If it does, then it’s valid. If not, then it’s invalid. You can use this method in your frontend system by using a simple code snippet that asks for permission from the user and performs a DNS query when they click on “yes”:
“`javascript
if(confirm(‘Do you own this email address?’)){
var dns = dnsLookup(‘mydomainname’).then((data) => {…});}else{alert(‘You need to own this email address.’);}“`
It’s hard to validate emails, but with some creativity it’s possible.
Email validation is hard. There are a lot of different ways to format an email address, and it’s difficult to tell which one is correct. To complicate things further, many email addresses aren’t verified at all—they could be someone’s first name, last name, or something else entirely. As a result of these difficulties, there are plenty of reasons why you might want to validate your user’s input in real time.
Conclusion
In conclusion, if you want to validate email addresses in real-time then it’s a pretty tough job. However, some solutions available will make the process easier for you. If you’re going to use regular expressions, check out our guide above on how exactly they work (or don’t) when trying to validate email addresses. It also might be worth considering an AJAX frontend system that provides immediate feedback on whether an address is valid or invalid; this way users can see at once whether their input was right or wrong without having to wait around for an entire page refresh before finding out what went wrong!
Add comment