简体   繁体   English

验证密码并重新输入密码

[英]Validating Password and Retype Password

I am having two textboxes like this :我有两个这样的文本框:

<p>
<input type="text" name="NPassword" placeholder="New Password"/></p>
<input type="text" name="RNPassword" placeholder="Retype New Password"/>

Now,what i want is that on every keypress of Retype New Password It should check if both are same or not.If yes then in green color display it on right side of RNPassword,Otherwise in red color display that both are not same and also disable the submit button of the page.How this can be done please help.现在,我想要的是在每次按键重新输入新密码时它应该检查两者是否相同。如果是,则以绿色显示它在 RNPassword 的右侧,否则以红色显示,两者都不相同,并且禁用页面的提交按钮。如何做到这一点请帮助。

Try this demo: ==> http://jsfiddle.net/uP8Q2/试试这个演示: ==> http://jsfiddle.net/uP8Q2/

Another demo = http://jsfiddle.net/ALk9Z/另一个演示 = http://jsfiddle.net/ALk9Z/

disable button demo http://jsfiddle.net/K2Xdc/禁用按钮演示http://jsfiddle.net/K2Xdc/

2 things to start with: 2件事开始:

Also next time add your JS code with the post :) rest should fit the need.下次在帖子中添加您的 JS 代码:)休息应该适合需要。

code代码

HTML HTML

<p>
    <input type="password" name="NPassword" placeholder="New Password" id="txtNewPassword" />
</p>
<input type="password" name="RNPassword" placeholder="Retype New Password" id="txtConfirmPassword" onChange="isPasswordMatch();" />
<div id="divCheckPassword"></div>

JS JS

    function isPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword) $("#divCheckPassword").html("Passwords do not match!");
    else $("#divCheckPassword").html("Passwords match.");
}

$(document).ready(function () {
    $("#txtConfirmPassword").keyup(isPasswordMatch);
});

Disable button demo code禁用按钮演示代码

 $('#hulk').prop('disabled' , true);
$('#txtConfirmPassword').on('keyup', function () {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword) {
        $("#divCheckPassword").html("Passwords do not match!");
    } else {
        $("#divCheckPassword").html("Passwords match.");
        $('#hulk').prop('disabled' , false);
    }
});

If you want to check that before submit you can do that in following way如果您想在提交之前检查,您可以通过以下方式进行

With plain Jquery:使用普通的 Jquery:

<p>
<input type="text" name="NPassword" id="first" placeholder="New Password"/></p>
<input type="text" name="RNPassword" id="second" placeholder="Retype New Password"/>
<span id="error" class="hidden">Not Matched"</span>

And then:进而:

EDIT ONCHANGE编辑更改

   $('#second').keyup(function(e){
           if($('#first').val() !== $('#second').val()){
                $('#error').removeClass('hidden');
                return false; 
           }else{
             $('#error').addClass('hidden');
           }
    });

http://liveweave.com/yVXnIF http://liveweave.com/yVXnIF

If you use KnockoutJs or AngularJs those have ability to bind verification in model itself.如果您使用 KnockoutJs 或 AngularJs,它们能够在模型本身中绑定验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM