Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

javascript - How can I make two message in password confirmation in jQuery validate?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...