Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Validation email rule

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 ?

like image 661
Preston Avatar asked Jun 30 '16 18:06

Preston


2 Answers

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,}" },
    }
});
like image 182
PacMan Avatar answered Oct 18 '22 17:10

PacMan


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
        }
    }
});
like image 39
baduga Avatar answered Oct 18 '22 17:10

baduga