错误 return false on keyup

error in return false on keyup

这是 html

    <form name="passwordchng" method="post" action="">
       <input name="_curpasswd" id="curpasswd" size="20" autocomplete="off" type="password" />
       <input name="_newpasswd" id="newpasswd" size="20" autocomplete="off" type="password" />
       <input name="_confpasswd" id="confpasswd" size="20" autocomplete="off" type="password" />
       <input type="submit" name="submit" value="Save" />
    </form>

这里是 jquery

  <script> 
    $("#newpasswd").keyup(function() {
        if($("#curpasswd").val()==$("#newpasswd").val()){
            $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
            return false;
        }
        else{
          $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character."); 
          return true;
        }
    });
 </script>

当 if 条件执行时,应该不允许提交表单。这里表单正在提交,即使两个密码相同...

您可以在 if 条件下禁用提交按钮:

 $('input[type="submit"]').attr('disabled','disabled');

并在 else 条件下再次启用:

 $('input[type="submit"]').removeAttr('disabled');

完整代码:

 $("#newpasswd").keyup(function() {
  if($("#curpasswd").val()==$("#newpasswd").val())
    {
    $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
    $('input[type="submit"]').attr('disabled','disabled');
}
else
{
    $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character."); 
    $('input[type="submit"]').removeAttr('disabled');
}});

如果您想 return 是真还是假,请在 submit 而不是 keyup 上使用验证。赞:

$("#passwordchng").submit(function() {

    if($("#curpasswd").val()==$("#newpasswd").val())
    {
        $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
        return false;
    }

    else
    {
        $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character.");
        return true;
    }

 });

See demo

Note: Id is given to form

试试这个..

<form name="passwordchng" method="post" action="">
  <input name="_curpasswd" id="curpasswd" size="20" autocomplete="off" type="password" />
   <input name="_newpasswd" id="newpasswd" size="20" autocomplete="off" type="password" onChange="password();"/>
    <input name="_confpasswd" id="confpasswd" size="20" autocomplete="off" type="password" />
  <input type="submit" name="submit" value="Save" />
    <div id="message"></div>
   </form>
<script>
function password() {

    var curpasswd = $("#curpasswd").val();
    var newpasswd = $("#newpasswd").val();

    if (curpasswd != newpasswd)
        $("#message").html("Passwords do not match!");
    else
        $("#message").html("Passwords match.");
}

$(document).ready(function () {
   $("#newpasswd").keyup(password);
});

</script>