HTML5 default email validation fails on domain part
I'm using a script to validate an email address. This is based on HTML5 validation and jQuery validation. Now I noticed that email addresses like name@hotmail.c
are validated correctly. However this email address is not a valid email address.
If I check this email address on syntax at through this website: http://cobisi.com/email-validation/validate-address, it tells me that the syntax is incorrect.
Email addresses with the domain .c
are not valid, however the default HTML5 and jQuery validator think they have a valid syntax.
I wrote a small example on JSFIDDLE: http://jsfiddle.net/kvALH/
Below you will find the email part of my jQuery validation that I use in the form.
$('#form-validate').validate({
rules: {
email: {
required: true,
email: true
}
},
How can I get this email validation working correctly? Do I need to write my own validator method for email?
Edit
I understand that a domain hotmail.c
could be correct, but the tld just doesn't exists. All tld's have 2 characters or more, so I'm wondering why the HTML5 validation allows 1 character in the domain!
To solve my problem i understand I need another validation so I used the regex from this thread (Validate email address in JavaScript?). Added a method to the jQuery validation and that seems to solve my issue.
JSFIDDLE: http://jsfiddle.net/kvALH/1/
jQuery.validator.addMethod("emailCustom", function (value, element, params) {
var re = /^(([^<>()[].,;:s@"]+(.[^<>()[].,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
return re.test(value);
}, "Please enter a valid email address.");
$('#form-validate').validate({
rules: {
email: {
required: true,
emailCustom: true
}
},
However I still think it's strange why the default validation just excepts this one character domain.
The JQuery validation only checks that the email address is syntactically valid. It does not check whether the domain name actually exists. To check for the existence of a TLD (the last part, eg ".com"), you need to make a list of the currently available TLD's (they are changing at regular intervals, so you need to keep up to date) and validate the email address against that yourself.
Take a look at this question for some possible ideas on how to write your own validation function: Validate email address in JavaScript?
You may also be interested in this: http://www.regular-expressions.info/email.html
In your case, you could adapt a regular expression to check the TLDs of your choice. For example:
^[A-Z0-9._%+-]+@[A-Z0-9.-]+.(?:[AZ]{2}|com|org|net|edu|gov|mil| biz|info|mobi|name|aero|asia|jobs|museum)$
(Taken from the regular-expressions.info link above.)
If you actually want to verify that it is possible to deliver email to an address, you will have to write code to perform an SMTP connection to the mail servers for the domain in question, and try the first steps of delivering an email (but then actually back out before you start sending any email data). Even this does not completely guarantee that you can deliver the email, because the mail server might reject your real email later or because of its contents. There is no 100% certain way to ensure that you can deliver an email, except possibly to actually deliver it.
As a last pointer, you can find a current list of TLD's here: http://www.icann.org/en/resources/registries/tlds
链接地址: http://www.djcxy.com/p/92928.html