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 get a validation error. But if we enter "test @teste", then the plugin shows that it is a valid email address.

here is the first example with

Now with

How can I check this field only if it is a valid email address?

+4
source share
2 answers

Think this answer several times here, but here is a link to one. Essentially, just use the regular javascript function with regular expression.

Email authentication using jQuery

+2
source

, , reg exp , ,

// 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');

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

All Articles