如何在jQuery验证中通过密码确认生成两条消息? [英] How can I make two message in password confirmation in jQuery validate?

查看:128
本文介绍了如何在jQuery验证中通过密码确认生成两条消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html代码如下:

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>

我用jquery验证的javascript代码验证如下:

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

演示和完整代码如下: http://jsfiddle.net/oscar11/fEZFB/609/

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

如果用户输入密码: abcdef ,然后点击按钮验证,有乱码:请确认你的密码

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

如果用户输入密码确认: ghijkl ,存在消息:请确认您的密码

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"

所以有两条消息:


  1. 如果用户没有输入密码确认,则会显示消息:请确认您的密码

  2. 如果用户输入密码确认,但与密码不一致,则显示消息:确认您的密码不一样

我该怎么办?

推荐答案

我认为这将涵盖您所寻找的内容。您不限于每个输入一条消息 - 您可以为每个输入设置一个消息。我在HTML中添加了一个中断以使其更具可读性。

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>

更新后的脚本:

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

我在密码中添加了必需的规则,因此单击带有空白表单的验证会生成一条消息。我在确认中删除了minlength - 它只需要等于密码,并且它具有最小长度。太短的密码有自己的消息,当密码足够长时,当确认字段为空或不等于密码时,您将收到不同的消息。你可以在这里看到它: http://jsfiddle.net/oscar11/fEZFB/609/

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/

这篇关于如何在jQuery验证中通过密码确认生成两条消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆