简体   繁体   English

密码匹配时,javascript禁用/启用提交按钮

[英]javascript disable/enable submit button when password match

i have this html 我有这个HTML

> <div class="row">
>                     <div class="col-xs-6 col-sm-6 col-md-6">
>                         <div class="form-group">
>                             <input type="password" name="pass1" id="pass1" class="form-control input-lg" placeholder="Password"
> tabindex="3">
>                         </div>
>                     </div>
>                     <div class="col-xs-6 col-sm-6 col-md-6">
>                         <div class="form-group">
>                             <input type="password" name="pass2" id="pass2"  onkeyup="checkPass(); return false;" class="form-control
> input-lg" placeholder="Confirm Password ">
>                            <span id="confirmMessage" class="confirmMessage"></span>
>                       </div>
>                       
>                     </div>
>                   
>                 </div>
>                 
>                 <div class="row">
>                     <input type="submit" id="login" value="Register">
>                   </div>

How can I do something like this: 我该如何做这样的事情:

When the password is empty the submit should be disabled (disabled="disabled"). 密码为空时,应禁用提交(disabled =“ disabled”)。

When something is typed in the passsword to remove the disabled attribute. 在密码中输入某些内容时,将禁用禁用的属性。

If the password field becomes empty again(the text is deleted) the submit button should be disabled again. 如果密码字段再次为空(文本已删除),则应再次禁用提交按钮。

When the password does not match,the submit button should be disable 如果密码不匹配,则应禁用提交按钮

I tried something like this: 我尝试过这样的事情:

 <script type="text/javascript">
function checkPass()
{
    //Store the password field objects into variables ...
    var pass1 = document.getElementById('pass1');
    var pass2 = document.getElementById('pass2');
    //Store the Confimation Message Object ...
    var message = document.getElementById('confirmMessage');
    //Set the colors we will be using ...
    var goodColor = "#66cc66";
    var badColor = "#ff6666";
    //Compare the values in the password field 
    //and the confirmation field
    if(pass1.value == pass2.value){
        //The passwords match. 
        //Set the color to the good color and inform
        //the user that they have entered the correct password 
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "Passwords Match!"
    }else{
        //The passwords do not match.
        //Set the color to the bad color and
        //notify the user.
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "Passwords Do Not Match!"
        $("#submit").attr("disabled", "disabled");
    }
}  
</script>

the javascript display message if password match or does not match but my problem is when the password does not match the SUBMIT button still go ahead and submit. javascript显示消息,如果密码匹配或不匹配,但是我的问题是当密码不匹配时,SUBMIT按钮仍然可以继续并提交。

You must be changing element property, not attribute: 您必须更改元素属性,而不是属性:

 $(document).ready(function() { var isDisabled = false; $('#toggler').click(function() { isDisabled = !isDisabled; $('#submit').prop('disabled', isDisabled); }); $('form').submit(function(e) { e.preventDefault(); alert('Submiting'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" method="post"> <button type="button" id="toggler">Disable/Enable</button> <button type="submit" id="submit">Submit</button> </form> 

Use something similar: 使用类似的东西:

if (pass1.value && pass2.value && pass1.value == pass2.value) {
    pass2.style.backgroundColor = goodColor;
    message.style.color = goodColor;
    message.innerHTML = "Passwords Match!"
    $("#submit").prop("disabled", false);
} else {
    pass2.style.backgroundColor = badColor;
    message.style.color = badColor;
    message.innerHTML = "Passwords Do Not Match!"
    $("#submit").prop("disabled", true);
}

I added two length checks (if value is "" it is evaluated as false), and added the #prop() call to enable the button, when the two strings match. 我添加了两个长度检查(如果value为"" ,则其评估为false),并在两个字符串匹配时添加了#prop()调用以启用按钮。

Sorry since i can't comment i'll answer instead If you have a form (i assume you do event though i can't see it), you can do it like this 抱歉,我无法发表评论,我会回答,如果您有表格(虽然我看不到,但我假设您有活动),您可以这样做

 $("#form").on('submit', function(e) {
  e.preventDefault();
    if(!$('#submit').prop('disabled')){
                $(this).submit();
     }

});

Basically i stop the submission first then check if the button is disabled before submitting the form. 基本上,我首先停止提交,然后在提交表单之前检查按钮是否已禁用。

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

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