jquery validation if input is empty

Viewed 30

How do I not use my one jQuery validation rule if the input field is empty?

I have two inputs on a form for additional email addresses, and have their required set to false, although I have another rule which checks if the email entered is a valid email. Even when the user does not enter an email, the message for my valid email rule is still appearing.

How do I only use the valid email rule if the input is not empty?

1 Answers
function additionalEmailEntered(){
        return $("#additionalEmail1" || "#additionalEmail2" ).val().length > 0;
    }

$('#registerNewClient').validate({
    rules:{
        additionalEmail1:{
            required: false,
            validate_email:{
                depends:additionalEmailEntered,
            }
        },
        additionalEmail2:{
            required: false,
            validate_email:{
                depends:additionalEmailEntered,
            }
        },
    },
    
                


Related