HTML email validation
So what exactly is an input type element? These input elements of type email allow the user to enter and modify an e-mail address.
The input value is validated to ensure that a properly formatted e-mail address populates the field before the form can be submitted.
On browsers that don't support inputs of type email, an email input type falls back to being a standard text input.
When setting the HTML input type with email address validation, you can also specify these additional attributes:
- list - The id of a datalist element located in the same document.
- maxlength - The maximum number of characters the user can enter into the email input.
- minlength - The minimum number of characters the user can enter into the email input.
- multiple - a boolean, that when true, allows the user can enter a list of multiple e-mail addresses, separated by commas and, optionally, whitespace characters.
- placeholder - This is suggested text that populates the email address by default.
- readonly - When this property is enabled, you cannot edit the input field, but only view the text that populates the field.
- size - The size attribute is a numeric value indicating how many characters wide the input field should be. The value must be a number greater than zero, and the default value is 20.
Validate emails with input type="email"
There are two levels of content validation available for email inputs. First, there's the standard level of validation offered to all inputs, which automatically ensures that the input type meets the requirements to be a valid email address.
However, there is also the option to add additional filtering to ensure that your own specialized needs are met, if you have any.
Checking for and invalid email address
If you need the entered email addresses to be restricted further than any string that seems like a valid email address, you can use the attribute, pattern, to specify a regular expression(Regex) the value must match for it to be valid.
Using an input pattern provides an extra level of email validation on top of the basic one provided by the input itself. You can learn more about regular expressions in this Email Regex guide. If the multiple attribute is specified, each individual item in the comma-separated list of values must match the regular expression. More on this in the next section.
Verifying multiple emails
If the multiple attribute is specified alongside the input element, you will be able to validate multiple addresses instead of a single input.
By adding this boolean multiple attribute, the input can be configured to accept multiple email addresses. Here’s the simplest possible example of using the multiple attribute:
The input is now considered valid when a single email address is entered, or when any number of email addresses is separated by commas and, optionally, some number of whitespace characters are present. When multiple is used, the value is allowed to be empty.
Here are examples of valid strings when multiple is specified:
- "firstname.lastname@example.org, email@example.com"
- "firstname.lastname@example.org,email@example.com, firstname.lastname@example.org"
Here are some examples of invalid strings and each invalid email address will be rejected:
- "email@example.com firstname.lastname@example.org"
Regex and the pattern attribute
Browsers that support the email input element allow for seemingly automic email validation. But what is going on in the background?The input element validation is equivalent to the following regular expression:
If you need the email address that was entered to be restricted further than simply any string that looks like an email address, you can use the attribute pattern to specify a regular expression that the entered text must match in order pass the validation.
If the multiple attribute is specified, each individual item in the comma-delineated list of values must match the regular expression.
HTML Form validation example
Let’s look at an example of how we can validate an HTML form. Imagine we have a company and we want to restrict the domain of our email address to be our company name.
Note, the domain of an email address is what comes after the @ symbol. For example the domain for the email address email@example.com is “gmail”.
Now let’s implement email address validation that restricts the email addresses to the domain “mycompany”.
We can validate against both the standard email address validation and the specified pattern, Look at the HTML form example below:
Should you use type=email or pattern?
Type email might be sufficient if you need to check that the text entered by the user into the input box takes the form and structure of any email address. If you need any additional validation on top of this I would use the attribute pattern.
For example, if you only want to accept email addresses from a particular domain, like “@mycompany.com”, you will want to use the attribute pattern to do this. No matter what method you use for your client side validation, you should also implement some form of server side validation. This will ensure the data that gets entered into your database is correct.
Using APIs to check for a valid email address
One alternative to using HTML input validation, is to use an API. One such API is the email validation and verification API from Abstract. This API provides a more thorough examination and will ensure tha a valid email address is entered.
You can learn the following information and perform the following checks using the Abstract API:
- Typo checking and smart suggestion for errors.
- Disposable and free email providers check allows you to only retain high-quality email addresses.
- The API is Privacy friendly being GDPR and CCPA compliant.
- The API can perform real-time MX & SMTP check ensuring the address is valid and in-use.
Abstract APIs are trusted by some of the biggest and best establishments in the industry such as Accenture, Google, and Standford.
If you think the our email validation service is a good fit for your specific project, you can try it for completely free and implement it in your software solution immediately!