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
                        },

4 Answers 4

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
                    },
5
  • an error here, in regex: return /^([a-zA-Z0-9_.\-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(value); Jan 22, 2013 at 11:30
  • 2
    @Tom : email validation regex is wrong. It should be return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value); Jul 23, 2014 at 13:17
  • 3
    could you please elaborate on why you wouldnt do this ?
    – wal
    Aug 3, 2015 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, 2016 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. Aug 20, 2018 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

5
  • 2
    In real life, everyone's email address has a .tld and only spammers have edge case characters.
    – Acyra
    Mar 10, 2016 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. Mar 10, 2016 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, 2017 at 3:41
  • @Mark just in one case... when your using email for tests: test@localhost Jan 30, 2017 at 9: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? Aug 21, 2018 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);
 });
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. Feb 15, 2017 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.