A simple good answer can be an input like this:
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*/
_x000D_
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-z0-9\-\._]){0,63}[a-z0-9]@(?![\-])((?!--)[a-z0-9\-]){0,63}[a-z0-9]\.(|((?![\-])((?!--)[a-z0-9\-]){0,63}[a-z0-9]\.))(|([a-z]{2,14}\.))[a-z]{2,14}$">
_x000D_
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
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:
^...$
from first till end(?![\.\-_])
not started with these: . - _((?!--)[a-z0-9\-])
accept a till z and numbers and - but not --((?![\-\._][\-\._])[a-z0-9\-\._])
from a till z lowercase and numbers and also . - _ accepted but not any kind of double of them.{0,63}
Length from zero till 63 (the second group [a-z0-9]
will fill the +1 but do not let the just character be . - _)@
The at sign(|(rule))
not exist or if exist should follow the rule. (For Subdomain and Second Level Extension)\.
DotExplaining of attributes:
type="email"
In modern browsers help also for valid email address
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