8

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 ?

3 Answers 3

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,}" },
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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
        }
    }
});

Comments

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

1 Comment

Thanks! I update the jquery.validation.js file with this: stackoverflow.com/a/30650715/588842, and works fine!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.