5

I am trying to troubleshoot and fix why my email validation is not properly working. It is suppose to detect an invalid email: where it had too many ...(dots), to still except European address (test@tom.uk.tk) as well as test@aol.com. But as of right now its excepting more that one dot, if you don't finish typing as long as it has the @ as long as you don't add the (.) dot. Will someone please help me with where I am going wrong?

<script>
     $("#form").validate({
    rules: {
        firstname_1: {
            required: true
        },
        email_1: {
            required: true,
            email: true
        },
        // Same for other fields
    },
    messages: {
        firstname_1: "This field is required.",
        email_1>: "Please enter valid email address.",
        // Repeat for other fields
    }
});
function isValidEmail(email_1)
{
    return /^[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}$/.test(email_1)
        && /^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*/.test(email_1);
}
</script>
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/ – Matt Evanoff Feb 01 '15 at 05:54
  • 1
    There's no hard limit to the number of `.`'s an email address or domain name can contain. What are you trying to accomplish? – Alexander O'Mara Feb 01 '15 at 05:55
  • Those addresses are technically valid, even if they don't exist. The only way to truly validate an email address is send it an email with a unique token, and have the user respond with it (usually via clicking a link). – Alexander O'Mara Feb 01 '15 at 05:59
  • Pretty much anything is valid AND not everybody follows the RFC. Send them an email. That is the only way to make sure it is valid. – Matt Evanoff Feb 01 '15 at 06:54

1 Answers1

9

Assuming you already have the correct validation logic and/or regex, then you would need to create your own rule using the .addMethod() method.

$("#form").validate({
    rules: {
        firstname_1: {
            required: true
        },
        email_1: {
            required: true,
            myEmail: true  // <- declare your custom rule
        },
        // Same for other fields
    },
    messages: {
        firstname_1: "This field is required.",
        email_1: "Please enter valid email address.",
        // Repeat for other fields
    }
});

// create your custom rule
jQuery.validator.addMethod("myEmail", function(value, element) {
    return this.optional(element) || /* your regex boolean logic goes here */;
}, 'Please enter valid email address.');

NOTE: You also had a syntax/spelling error on a field name within messages. Should be email_1, not email_1>.

See this answer for the proper way to use .addMethod().

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Note this regex doesn't include capital letters in the email address. Nor does it allow a '+' symbol, like via gmail usage. – matias elgart Apr 08 '19 at 15:12
  • Totally get it, I'm just letting people who read your answer know that if they use the regex in your answer because they're lazy, that it still needs some modifications before it's ready for use. Your answer of course is perfectly valid (I used it, thanks!) – matias elgart Apr 08 '19 at 16:43
  • @matiaselgart, ok. In that case, I removed the regex from my answer because it's irrelevant. – Sparky Apr 08 '19 at 20:04