7

With this simple email rule:

$("#loginCliente").validate({
    rules: {
        loginNomEmail: { required: true, email: true },
    }
});

In this input field, if we enter "test@" we got the validate error. But if we enter "test@teste" the plugin show that is an valid email.

here is the first example with "test@"

Now with "test@test". As can see, without error.

How I can validate this field only if it is an valid email ?

Preston
  • 2,135
  • 7
  • 29
  • 47

3 Answers3

14

i was having the same issue before and i think that adding a reg exp in the rules like this may be helpful

// this function is to accept only email
    jQuery.validator.addMethod("accept", function(value, element, param) {
        return value.match(new RegExp("^" + param + "$"));
    },'please enter a valid email');

and in your rules you can just add this

$("#loginCliente").validate({
    rules: {
        loginNomEmail: { required: true,
                         email: true,
                         accept:"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}" },
    }
});
PacMan
  • 1,358
  • 2
  • 12
  • 25
4

You can add your own method to validator:

jQuery.validator.addMethod("emailExt", function(value, element, param) {
    return value.match(/^[a-zA-Z0-9_\.%\+\-]+@[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,}$/);
},'Your E-mail is wrong');

and use "emailExt" instead of "email":

$("#loginCliente").validate({
    rules: {
        loginNomEmail: { 
            required: true,
            emailExt: true
        }
    }
});
baduga
  • 233
  • 1
  • 8
2

Think this is answered a few times around here but here's a link to one. Essentially just use a regular old javascript function with regex.

Email validation using jQuery

Community
  • 1
  • 1
Raymondim
  • 96
  • 3
  • 1
    Thanks! I update the jquery.validation.js file with this: http://stackoverflow.com/a/30650715/588842, and works fine! – Preston Jun 30 '16 at 18:44