0

I'm using jquery.validate.js to validate the Email Input of the Formular.

It's working well, but I'm suprised that it's excepting emails as valid with the following format: name@domain

In my opinion there is missing a .TLD, no?

I checked the function inside the jquery.validate.js file. They are referencing the specification from www.whatwg.org for a valid email Adress. On the site it says that a valid email address should have the format name@doman.tld

The JS Regex inside jquery.validate.js is the following:

email: function( value, element ) {
    return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );
},

How do I use a custom Regex that validates if the email includes a .TLD?

I specified the custom validation method as following:

jQuery.validator.addMethod("customemail", function(value, element) {
    return this.optional(element) || /^(([^<>()[\]\\.,;:\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,}))$/.test(value);
}, "Please enter a valid email address.");

The function to validate the form is the following:

$("#subscription-form").validate({
});

What do I need to include, so the script uses my custom method instead of the email method?

Snowball
  • 1,402
  • 2
  • 17
  • 31
  • 1
    Please complain and/or report any bugs directly to the developer at his [GitHub page](https://github.com/jzaefferer/jquery-validation). That's how his `email` method works and there's really nothing we can do about it here. Of course, if you're asking how to write a custom method that works differently than the default, that's an acceptable question. – Sparky Jul 08 '14 at 05:42
  • The whatwg site specifically provides a JS regex, which matches what you've posted: http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#valid-e-mail-address – Ian Jul 08 '14 at 05:45
  • FYI, the default `email` method used to require a TLD in jQuery Validate version 1.11.0 and apparently was intentionally changed in 1.12.0. – Sparky Jul 08 '14 at 05:46
  • @lan the jquery.validate.js is using the JS regex provided on that site, because of that I was referencing it. But seems like this rexed is just validating if there is an "@" included and dont checks for the TLD. – Snowball Jul 08 '14 at 05:48
  • 3
    This question appears to be off-topic because it is a complaint or bug report about the default behavior of the plugin. – Sparky Jul 08 '14 at 05:48
  • @Sparky thanks for the info. As I asked before, why is it common practice just to validate for an included "@" and not for the TLD? Is there anything im missing out? – Snowball Jul 08 '14 at 05:49
  • And indeed I was asking for the custom method, since I didnt find an answer to a similiar question before. The JS Regex @Moeed Farooqui provided works great – Snowball Jul 08 '14 at 05:50
  • Again, you'll have to ask the developer why he changed this method. If you want to do it the other way, downgrade the plugin to version 1.11.0 or write a custom method. – Sparky Jul 08 '14 at 05:50
  • If you're asking for a custom method, then edit your question to ask for a custom method, rather than, _"So why isn't it working?"_ – Sparky Jul 08 '14 at 05:52
  • **Another FYI: [Version 1.12.0 Changelog](https://github.com/jzaefferer/jquery-validation/releases/tag/1.12.0)**: _"Methods: Update email to use HTML5 regex, remove email2 method"_ – Sparky Jul 08 '14 at 05:56
  • 1
    You should never _"edit the script to suit your needs"_. Simply create a new method using the desired regex: http://jqueryvalidation.org/jQuery.validator.addMethod/ – Sparky Jul 08 '14 at 05:59
  • Thanks again @Sparky, this is obviously best practice. I edited my question because Im not sure how to use the specified custom Method – Snowball Jul 08 '14 at 06:17
  • Where/how do you declare your rules? Simply use `customemail` in place of `email`. – Sparky Jul 08 '14 at 07:03

1 Answers1

1

Try this Regex:

/^(([^<>()[\]\\.,;:\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,}))$/
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
  • great, seems to work. Thank you! I saw on other posts that its common practice to just validate if there is an "@" included and then send a confirmation email to validate the address. I send that confirmation email anyway, but I was asking myself why its not common practice to check also for the TLD? – Snowball Jul 08 '14 at 05:45