A simple good answer can be an input like this:
2021 UPDATED & Support IE10+
^(?![_.-])((?![_.-][_.-])[a-zA-Z\d_.-]){0,63}[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}$
input:not(:placeholder-shown):invalid{
background-color:pink;
box-shadow:0 0 0 2px red;
}
/* :not(:placeholder-shown) = when it is empty, do not take as invalid */
/* :not(:-ms-placeholder-shown) use for IE11 */
/* :invalid = it is not followed pattern or maxlength and also if required and not filled */
/* Note: When autocomplete is on, it is possible the browser force CSS to change the input background and font color, so i used box-shadow for second option*/
Type your Email:
<input
type="email"
name="email"
lang="en"
maxlength="254"
value=""
placeholder="[email protected]"
autocapitalize="off" spellcheck="false" autocorrect="off"
autocomplete="on"
required=""
inputmode="email"
pattern="(?![_.-])((?![_.-][_.-])[a-zA-Z\d_.-]){0,63}[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}">
According to the following:
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email
- Maximum length of domain in URL (254 Character)
- Longest possible Subdomain(Maybe =0-64 character), Domain (=0-64 character), First Level Extension(=2-14 character), Second Level Extension(Maybe =2-14 character) as @Brad motioned.
- Avoiding of not usual but allowed characters in Email name and just accepting usual characters that famous free email services like Outlook, Yahoo, Gmail etc. will just accept them. It means accepting just : dot (.), dash (-), underscore (_) just in between a-z (lowercase) or A-Z (uppercase - just because it is common - thanks to Badri Paudel) and numbers and also not accepting double of them next to each other and maximum 64 characters.
Note: Right now, longer address and even Unicode characters are possible in URL and also a user can send email to local or an IP, but i think still it is better to not accepting unusual things if the target page is public.
Explain of the regex:
(?![_.-])
not started with these: _ . -
((?!--)[a-zA-Z\d-])
accept a till z and A till Z and numbers and - (dash) but not --
((?![_.-][_.-])[a-zA-Z\d_.-])
from a till z lowercase and A till Z uppercase and numbers and also _ . - accepted but not any kind of double of them.
{0,63}
Length from zero till 63 (the second group [a-zA-Z\d]
will fill the +1 but just do not let the character be _ . -)
@
The at sign
(rule){1,2}
this rule should exist 1 or 2 times. (for Subdomain & Domain)
(rule)?
or ((rule)|)
not exist or if exist should follow the rule. (for Second Level Extension)
\.
Dot
Note: For being more strict about uppercase
you can remove all A-Z
from the pattern.
Note: For being not strict about Persian/Arabic numbers
٠١٢٣٤٥٦٧٨٩
۰۱۲۳۴۵۶۷۸۹
you can add \u0660-\u0669\u06f0-\u06f9
next to all \d
in the pattern.
Try the RegEx: https://regexr.com/64kjf
Note: Using ^...$
is not necessary in input pattern, but for general RegEx testing will be needed. It means start / end of the string should be same as the rule, not just a part.
Explaining of attributes:
type="email"
or type="text"
(email
In modern browsers will help for the validation also it don't care spaces in start or end for the validation or getting value)
name="email" autocomplete="on"
To browser remember easy last filled input for auto completing
lang="en"
Helping for default input be English
inputmode="email"
Will help to touch keyboards be more compatible
maxlength="254"
Setting the maximum length of the input
autocapitalize="off" spellcheck="false" autocorrect="off"
Turning off possible wrong auto correctors in browser
required=""
This field is required to if it was empty or invalid, form be not submitted
pattern="..."
The regex inside will check the validation
\w
=a-zA-Z\d_
so:
Lightest version
(?![_.-])((?![_.-][_.-])[\w.-]){0,63}[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}
foo@bar
as valid. While it may be technically valid (for example,foo@localhost
is a valid email), for most real world use cases, it's not going to work, and users may end up not getting emails as they've missed the .com (or whatever) off – Oujda