1

I am using JQuery validate to validate that two fields are filled in, and that they match.

$("#resetFormDo").validate({
    rules: {
        password: {
            required: true
        },
        passwordCopy: {
            required: true,
            equalTo: '#password'
        },
    },
    messages: {
        password: "Both fields required",
        passwordCopy: "Both fields required"
    },
});

Question: how to add a different error message for when fields don't match?

With it set like above, it just gives same error message for passwordCopy field if it's either empty or does not match other password field. Do I simply have to make the message more generic e.g. "Both fields required and must match"? Or, is there a way to separate them?

1 Answer 1

2

I think this will address it:

$("#resetFormDo").validate({
    rules: {
        password: {
            required: true,
        },
        passwordCopy: {
            required: true,
            equalTo: "#password"
        }
    },
    messages: {
        password: {
            required: "Please provide a password",
        },
        passwordCopy: {
            required: "Please provide a password",
            equalTo: "Both fields must match"
        }
    }
});
 });
1
  • Thats cool, didn't know you could do that! Thanks so much, appreciated. Will accept when I can
    – StudioTime
    Commented Jun 9, 2014 at 17:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.