1

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.

Community
  • 1
  • 1
Timo002
  • 3,138
  • 4
  • 40
  • 65

1 Answers1

3

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, e.g. ".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.-]+\.(?:[A-Z]{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

Community
  • 1
  • 1
Fabian Fagerholm
  • 4,099
  • 1
  • 35
  • 45
  • If I use the regex in the link you mention, then the email adres `name@hotmail.c` also failes! I found that one to, but I think it's strange that default HTML5 just passes these email addresses. – Timo002 Mar 11 '14 at 08:41
  • There is nothing strange about this, because – as has already been mentioned – this is only a general syntax validation, not a verification. And `hotmail.c` is a _technically_ perfectly valid domain name. – CBroe Mar 11 '14 at 08:45
  • @Timo002: I added some more information and an example of a regexp that you can modify to your needs. – Fabian Fagerholm Mar 11 '14 at 08:47
  • @CBroe: Good point, I improved the answer to make it clearer that this is expected behaviour. – Fabian Fagerholm Mar 11 '14 at 08:52
  • @CBroe, I understand that `hotmail.c` could be a technically perfectly domain name, however this tld does not exists, so why is HTML5 email validation is passing this? We live in 2014 so why not just check if the tld even exists. BTW, there isn't even one single tld that exists of just 1 character. It's al 2 characters or more! – Timo002 Mar 11 '14 at 08:52
  • 1
    _“so why is HTML5 email validation is passing this”_ – because it is not its _job_ to check for what you call “existing” domains – that simple. In my _local_ network, `foo.extension` might be an “existing” domain, and therefor I could also use `info@foo.extension` there – and it would be just _stupid_ if an HTML input field of type=email would _not_ allow me to input that email address, because I actually want to and _can_ successfully use it in _my_ network. – CBroe Mar 11 '14 at 08:55
  • @CBRoe, that means that you can create your own extentions in you local network? Didn't know that! – Timo002 Mar 11 '14 at 08:59
  • You don’t need to “create extensions” – you just have to tell your DNS resolving mechanism (f.e. your `hosts` file, if you have no pecific DNS service running) to resolve that domain address to an IP. F.e. when you are using `localhost`, that is what you are doing already. And using that same mechanism for domain names such as `development.local` or `foo.bar` is exactly the same thing. – CBroe Mar 11 '14 at 09:05
  • Great answer. Anyone using this for javascript will want to add the case-insensitive flag "i", i.e..... "/.....jobs|museum)$/i" – Alkanshel Oct 02 '14 at 21:50