验证旧密码字段

时间:2018-11-02 05:28:08

标签: jquery

如果未输入旧密码,则卡住了如何应用验证。如果用户仍然忘记输入旧密码,则需要验证。预先感谢。

FIDDLE DEMO

$(document).ready(function() {
          $("#submitBtn").click(function(){
              validate();
          });
        });

        function validate() {
          var password1 = $("#password1").val();
          var password2 = $("#password2").val();
            if(password1 != password2) {
               $(".error-msg").html("Password and confirmation password do not match.").show();                    
            }
            else {
                $(".error-msg").html("").hide();  
                ValidatePassword();
            }
        }

        function ValidatePassword() {
          var regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,}$/;
          var txt = document.getElementById("password1");
          if (!regex.test(txt.value)) {
              $(".error-msg").html("The password does not meet the password policy requirements.").show();
          } else {
              $(".error-msg").html("").hide();
              window.location.href='change-password-msg.html';
          }
        }

2 个答案:

答案 0 :(得分:2)

您必须首先检查old password input是否已填写。请查看我的代码,它可以为您提供帮助。观看此直播JSFiddle

HTML代码-

<div class="error-msg"></div>
<br><br>

<form>
  <div class="form-group">
    <label>OLD PASSWORD</label>
    <input name="oldPassword" id="oldPassword" type="password" class="form-control">
  </div>
  <div class="form-group">
    <label>NEW PASSWORD</label>
    <input type="password" id="password1" class="form-control">
  </div>
  <div class="form-group">
    <label>CONFIRM PASSWORD</label>
    <input type="password" id="password2" class="form-control">
  </div>
</form>
<button type="button" id="submitBtn" class="btn btn-primary">UPDATE</button>

CSS代码-

.error-msg {
  width: 100%;
  font-family: 'nobelregular';
  color: #ff0002;
  display: none;
}

JS代码-

$(document).ready(function() {
  $("#submitBtn").click(function() {
    var oldVal = $('#oldPassword').val() || "";
    if (oldVal == "") {
      $(".error-msg").html("First you have to fill Old Password.").show();
      return false;
    }
    validate();
  });
});

function validate() {
  var password1 = $("#password1").val();
  var password2 = $("#password2").val();
  if (password1 != password2) {
    $(".error-msg").html("Password and confirmation password do not match.").show();
  } else {
    $(".error-msg").html("").hide();
    ValidatePassword();
  }
}

function ValidatePassword() {
  var regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,}$/;
  var txt = document.getElementById("password1");
  if (!regex.test(txt.value)) {
    $(".error-msg").html("The password does not meet the password policy requirements.").show();
  } else {
    $(".error-msg").html("").hide();
    window.location.href = 'change-password-msg.html';
  }
}

答案 1 :(得分:1)

MessageHandler
$(document).ready(function() {
			$("#old-password").change(function(){
       var value = $("#old-password").val();
     if(value===''){
     alert("please enter Old Password");
     }
      
      });
          $("#submitBtn").click(function(){
              validate();
          });
        });

        function validate() {
         var value = $("#old-password").val();
           if(value===''){
           
           alert("please enter Old Password");
           }
          var password1 = $("#password1").val();
          var password2 = $("#password2").val();
            if(password1 != password2) {
               $(".error-msg").html("Password and confirmation password do not match.").show();                    
            }
            else {
                $(".error-msg").html("").hide();  
                ValidatePassword();
            }
        }

        function ValidatePassword() {
          var regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,}$/;
          var txt = document.getElementById("password1");
          if (!regex.test(txt.value)) {
              $(".error-msg").html("The password does not meet the password policy requirements.").show();
          } else {
              $(".error-msg").html("").hide();
              window.location.href='change-password-msg.html';
          }
        }
.error-msg {
    width: 100%;
    font-family: 'nobelregular';
    color: #ff0002;
    display: none;
}
您可以像此代码片段一样对旧密码进行验证,如果您想在客户端验证旧密码,也可以使用ajax调用来验证该密码