18

I find that jQuery validation plugin regex to be insufficient for my requirement. It accepts any email address xxx@hotmail.x as a valid email address whereas I want to be able to supply this regex /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/ so that it would validate complete .com part of the address. I'm more concerned about being able to supply my own regex than getting a fool proof regex(as there is no fool proof regex for email validation)

Just FYI: I'm also doing server side validation but at this point I'm not worried about which email address regex is right.

Is there a way to do that within the "rules" section of jQuery validate plugin?

This is my rules section right now:

rules: {
                        email: {
                            required:  {
                                    depends:function(){
                                        $(this).val($.trim($(this).val()));
                                        return true;
                                    }   
                                },
                            email: true
                        },
Sparky
  • 98,165
  • 25
  • 199
  • 285
neelmeg
  • 2,459
  • 6
  • 34
  • 46

4 Answers4

25

I wouldn't do this but for the sake of an answer you need to add your own custom validation.

//custom validation rule
$.validator.addMethod("customemail", 
    function(value, element) {
        return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
    }, 
    "Sorry, I've enabled very strict email validation"
);

Then to your rules add:

rules: {
                    email: {
                        required:  {
                                depends:function(){
                                    $(this).val($.trim($(this).val()));
                                    return true;
                                }   
                            },
                        customemail: true
                    },
RouR
  • 6,136
  • 3
  • 34
  • 25
Tom
  • 2,604
  • 11
  • 57
  • 96
  • an error here, in regex: return /^([a-zA-Z0-9_.\-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(value); – Andrey Vorobyev Jan 22 '13 at 11:30
  • 2
    @Tom : email validation regex is wrong. It should be **`return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);`** – Prashant Tapase Jul 23 '14 at 13:17
  • 3
    could you please elaborate on why you wouldnt do this ? – wal Aug 03 '15 at 21:35
  • 1
    Yes the regex produces errors. I'm using istead the following `return /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i.test(value);` – Hexodus Feb 13 '16 at 19:18
  • @Tom Strict validation is good for live websites. test@localhost is definitely spam on a live website. Calling it "very strict email validation" isn't appropriate. – Supreme Dolphin Aug 20 '18 at 15:41
6

Your regex is simply too strict, jQuery is right.

"this is a valid adress !"@yes.it.is

I suggest you to read this : Stop Validating Email Addresses With Your Complex Regex

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    In real life, everyone's email address has a .tld and only spammers have edge case characters. – Acyra Mar 10 '16 at 15:54
  • @Acyra You're free to write code which works only sometimes and by accident. But many coders prefer to write code which follows standard and which works with all emails, not just the ones you've already seen. – Denys Séguret Mar 10 '16 at 16:27
  • It is out of my knowledge. Is email like xxx@xxx technically possible? (no .xx suffix). The default validation plugin allow it. – Mark Jan 25 '17 at 03:41
  • @Mark just in one case... when your using email for tests: test@localhost – jtmielczarek Jan 30 '17 at 09:25
  • Email providers do not allow for registration of such emails. Secondly, if you're registering emails on your own domain, and your email is rejected by my website, you can always create a new address if you need the services of my website. Third, why would you want to create a use such a weird email publicly? My site wouldn't be the first, and definitely not the last to reject such email addresses. Fourth, if I'm hosting a party, and I say the dress code is white, is it okay to come in black and complain that I'm too strict? – Supreme Dolphin Aug 21 '18 at 17:37
1

Try this!

   jQuery.validator.addMethod("customEmail", function(value, element) {
             return this.optional(element) || /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i.test(value);
            }, "Please enter valid email address!");

        $(form).validate({
             rules: {
                 email:{
                     required:true,
                     customEmail:true
                 }
             }
        });
0
// Add Custom Email Validation
jQuery.validator.addMethod('customemail', function (emailaddr, element) {
      emailaddr = emailaddr.replace(/\s+/g, '');
      return this.optional(element) || 
      emailaddr.match(/^\b[A-Z0-9._%-]+@@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i);
 });
JosephT
  • 1
  • 1
  • While this code may answer the question, please consider providing some explanation to help the OP understand the solution, and to help other readers who may have a similar problem. – CodeMouse92 Feb 15 '17 at 18:29