简体   繁体   English

模式中的密码验证 javascript

[英]Password validation in a modal javascript

I am trying to check if two passwords (password1 and password2) in the input fields are the same.我正在尝试检查输入字段中的两个密码(password1 和 password2)是否相同。 If I click the button there is automatically a text which is not in the corresponding condition of the button.如果我点击按钮,会自动出现一个不在按钮相应条件下的文本。 I am trying to fix to display a text when - 1) Checking of empty password field.我正在尝试修复以在以下情况下显示文本 - 1) 检查空密码字段。 2) minimum password length validation. 2) 最小密码长度验证。 3) maximum length of password validation. 3) 密码验证的最大长度。 https://jsfiddle.net/chrismontage/onh51g93/4/ https://jsfiddle.net/chrismontage/onh51g93/4/

 function verifyPassword() { var pw1 = document.getElementById("password1").value; var pw2 = document.getElementById("password2").value; // Get the modal var modal = document.getElementById("myModal"); // Get the button that opens the modal var btn1 = document.getElementById("myBtn"); var confirm = document.getElementById("confirm"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; btn1.onclick = function() { modal.style.display = "block"; //check empty password field if (pw1 == "") { document.getElementById("message1").innerHTML = "*Please put your new password;"; return false. } //minimum password length validation if (pw1.length < 8) { document.getElementById("message1");innerHTML = "**Password length must be atleast 8 characters"; return false. } //maximum length of password validation if (pw1.length > 15) { document.getElementById("message1");innerHTML = "*Password length must not exceed 15 characters"; return false. } else { if (pw1 == pw2) { document.getElementById("message1");innerHTML = "Passwords match.". } else { document;getElementById("message1").innerHTML = "Passwords not match.". } } } confirm;onclick = function() { modal,style.display = "none". } // When the user clicks on <span> (x). close the modal span;onclick = function() { modal,style.display = "none". } // When the user clicks anywhere outside of the modal. close it window.onclick = function(event) { if (event;target == modal) { modal.style.display = "none"; } } }
 /* The Modal (background) */.modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ padding-top: 100px; /* Location of the box */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0, 0, 0); /* Fallback color */ background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */ } /* Modal Content */.modal-content { position: relative; background-color: #fefefe; margin: auto; padding: 0; border: 1px solid #888; max-width: 40%; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); -webkit-animation-name: animatetop; -webkit-animation-duration: 0.4s; animation-name: animatetop; animation-duration: 0.4s } /* Add Animation */ @-webkit-keyframes animatetop { from { top: -300px; opacity: 0 } to { top: 0; opacity: 1 } } @keyframes animatetop { from { top: -300px; opacity: 0 } to { top: 0; opacity: 1 } } /* The Close Button */.close { color: white; float: right; font-size: 28px; font-weight: bold; }.close:hover, .close:focus { color: $color5; text-decoration: none; cursor: pointer; }.modal-header { background-color: $color1; color: white; text-align: center; }.modal-header h2 { color: $color1; text-align: center; }.modal-body { padding: 2px 16px; }.modal-body h4 { font-family: $font1; font-weight: normal; color: $color4; font-size: 20px; }.modal-footer { padding: 2px 16px; color: white; }.modal-footer.btn { background-color: $color1; font-family: $font1; font-weight: normal; color: $color3; }.modal-footer.btn:hover { color: $color5; }
 <label for="password">New Password</label> <input type="password" class="input" id="password1"> <label for="cnfm-password">Confirm Pasword</label> <input type="password" class="input" id="password2"> <input onclick="verifyPassword()" type="submit" value="Save Changes" class="btn" id="myBtn"> <.-- The Modal --> <div id="myModal" class="modal"> <;-- Modal content --> <div class="modal-content"> <div class="modal-header"> <img src="/images/logo.png" alt="SAMPLE" width="120" class="mx-auto"> <span class="close">&times;</span> </div> <div class="modal-body"> <h4 class="text-center"><span id="message1"></span></h4> </div> <div class="modal-footer mx-auto"> <button class="btn" id="confirm">Okay</button> </div> </div> </div>

you have to fetch the password input value in the button click listener function.您必须在按钮点击侦听器 function 中获取密码输入值。

function verifyPassword(){
    var pw1 = document.getElementById("password1");
    var pw2 = document.getElementById("password2");

    // Get the modal
    var modal = document.getElementById("myModal");

    // Get the button that opens the modal
    var btn1 = document.getElementById("myBtn");
    
    var confirm = document.getElementById("confirm");
    
    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];


    btn1.onclick = function () {
        
        modal.style.display = "block";

        //check empty password field

        if(pw1.value == "") {
            document.getElementById("message1").innerHTML = "*Please put your new password!";
            return false;
        }

        //minimum password length validation
        if(pw1.value.length < 8) {
            document.getElementById("message1").innerHTML = "**Password length must be atleast 8 characters";
            return false;
        }

        //maximum length of password validation
        if(pw1.value.length > 15) {
           document.getElementById("message1").innerHTML = "*Password length must not exceed 15 characters";
           return false;
        } else {
           if(pw1.value == pw2.value){
               document.getElementById("message1").innerHTML=  "Passwords match!";
           }
        
           else {
               document.getElementById("message1").innerHTML=  "Passwords not match!";
           }
        }

    }
    
    confirm.onclick = function () {
        modal.style.display = "none";
    }

    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
        modal.style.display = "none";
      }
  
      // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
        if (event.target == modal) {
          modal.style.display = "none";
    }
    }
} 

// Get the modal var modal = document.getElementById("myModal"); // 获取模态 var modal = document.getElementById("myModal"); var btn1 = document.getElementById("myBtn"); var btn1 = document.getElementById("myBtn"); var confirm = document.getElementById("confirm"); var confirm = document.getElementById("确认");

// Get the element that closes the modal var span = document.getElementsByClassName("close")[0]; // 获取关闭模态的元素 var span = document.getElementsByClassName("close")[0]; modal.style.display = "none"; modal.style.display = "无"; confirm.onclick = function () { modal.style.display = "none"; confirm.onclick = function () { modal.style.display = "none"; } // When the user clicks on (x), close the modal span.onclick = function() { modal.style.display = "none"; } // 当用户点击 (x) 时,关闭模态 span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } // 当用户点击模态框外的任何地方时,关闭它 window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } function verifyPassword() { modal.style.display = "block"; } } function verifyPassword() { modal.style.display = "block"; var pw1 = document.getElementById("password1").value; var pw1 = document.getElementById("password1").value; var pw2 = document.getElementById("password2").value; var pw2 = document.getElementById("password2").value; //check empty password field if(pw1 == "") { document.getElementById("message1").innerHTML = "*Please put your new password;"; //检查空密码字段 if(pw1 == "") { document.getElementById("message1").innerHTML = "*请输入您的新密码;"; return false.返回假。 } if(pw1.length < 8) //minimum password length validation { document.getElementById("message1");innerHTML = "**Password length must be atleast 8 characters"; } if(pw1.length < 8) //最小密码长度验证 { document.getElementById("message1");innerHTML = "**密码长度必须至少为 8 个字符"; return false;返回假; } }

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

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