1

My html code like this :

<form class="validatedForm" id="commentForm" method="get" action="">
    <fieldset>
        <input name="password" id="password" />
        <input name="password_confirmation" />
    </fieldset>
</form>
<button>Validate</button>

My javascript code to validate with jquery validate like this :

jQuery('.validatedForm').validate({
    rules: {
        "password": {
            minlength: 6
        },
        "password_confirmation": {
            minlength: 6,
            equalTo : "#password"
        }
    },
      messages: {
            "password": 'Please enter a password, minimal 6 characters',
            "password_confirmation": 'Please confirm your password'
    },
});

Demo and full code like this : http://jsfiddle.net/oscar11/fEZFB/609/

If user input password : abcdef, then click button validate, there exist messsage : "Please confirm your password"

If user input password confirmation : ghijkl, there exist message : "Please confirm your password"

I want to change the message if user input password confirmation not same

The message like this : "confirm your password is not the same"

So there exist two message :

  1. If user not input password confirmation, the message : "Please confirm your password"
  2. If user input password confirmation, but not same with password, the message : "confirm your password is not the same"

How can I do it?

1 Answer 1

0

I think this will cover what you're looking for. You aren't limited to one message per input - you can set one for each rule on each input. I added a break in your HTML to make it more readable.

<form class="validatedForm" id="commentForm" method="get" action="">
    <fieldset>
        <input name="password" id="password" /><br/>
        <input name="password_confirmation" id="password_confirmation" />
    </fieldset>
</form>
<button>Validate</button>

Updated script:

jQuery('.validatedForm').validate({
    rules: {
        "password": {
            minlength: 6,
            required: true
        },
        "password_confirmation": {
            equalTo: "#password"
        }
    },
      messages: {
            "password": {
            minLength: "Password must be at least 6 charachters",
            required: "Password is required."
        },
            "password_confirmation": {
            equalTo: "The password and confimation fields don't match"
        }
    },
});

$('button').click(function () {
    console.log($('.validatedForm').valid());
});

I added a required rule to the password so clicking validate with a blank form generates a message. I removed the minlength on the confirmation- it only needs to be equal to the password, and it has a minlength. Too short of a password has its own message, and when the password is long enough you'll get a different message when the confirmation field is empty or otherwise not equal to the password. You can see it in the fiddle here: http://jsfiddle.net/oscar11/fEZFB/609/

Sign up to request clarification or add additional context in comments.

3 Comments

password and password_confirmation is not required
Remove the required rule and message?
It seems you do not understand what I mean. But it's okay. I've found the answer

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.