It is said "With HTML5, we need no more js or a server side code to check if the user's input is a valid email or url address"
How can I validate email after a user enter? and without JS how to display a message if the user enter a wrong form of his/her email address.
<input type="email" pattern="[^ @]*@[^ @]*" placeholder="Enter your email">
<input type="submit" value="Submit">
Here is the example I use for all of my form email inputs. This example is ASP.NET, but applies to any:
HTML5 still validates using the pattern when not required. Haven't found one yet that was a false positive.
This renders as the following HTML:
I know you are not after the Javascript solution however there are some things such as the customized validation message that, from my experience, can only be done using JS.
Also, by using JS, you can dynamically add the validation to all input fields of type email within your site instead of having to modify every single input field.
It is very difficult to validate Email correctly simply using HTML5 attribute "pattern". If you do not use a "pattern" someone@ will be processed. which is NOT valid email.
Using
pattern="[a-zA-Z]{3,}@[a-zA-Z]{3,}[.]{1}[a-zA-Z]{2,}[.]{1}[a-zA-Z]{2,}"
will require the format to besomeone@email.com
however if the sender has a format likesomeone@email.net.au
(or similar) will not be validated to fix this you could putpattern="[a-zA-Z]{3,}@[a-zA-Z]{3,}[.]{1}[a-zA-Z]{2,}[.]{1}[a-zA-Z]{2,}[.]{1}[a-zA-Z]{2,}"
this will validate ".com.au or .net.au or alike.However using this, it will not permit someone@email.com to validate. So as far as simply using HTML5 to validate email addresses is still not totally with us. To Complete this you would use something like this:
or:
However, I do not know how to validate both or all versions of email addresses using HTML5 pattern attribute.
The
input type=email
page of the www.w3.org site notes that an email address is any string which matches the following regular expression:Use the
required
attribute and apattern
attribute to require the value to match the regex pattern.The only 100% correct method is to check for @-sign somewhere in the entered email address and then posting a validation message to given email address. If they can follow validation instructions in the email message, the inputted email address is correct.
David Gilbertson wrote about this years ago:
In addition, some email addresses that may be syntactically or politically invalid, do work. For example,
postmaster@ai
does technically work even though TLDs should not have MX records. Also see discussion about email validation on the WHATWG mailing list (where HTML5 is designed in the first place).Using
[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}
forsomemail@email.com
/somemail@email.com.vn