如何判断一个密码是否等于另一个密码?

How to tell is a password is equal to another password?

我正在编写注册表单,但我希望访问者输入密码两次。如果它们相同,那没关系,但如果它们不相同,它们就会出现一个弹出窗口。表格在 HTML 和 JavaScript 中。这是我的基本形式(实际上不起作用,但它是基本的。)

<!doctype html> 
     <html>
       <head>       
          <title>Register</title> 
       </head>      
       <body> 
           <form> 
               <fieldset> 
                    <label for="uName"> 
                          Username:* 
                          <input type="text" id="uName"  placeholder="Enter a username..." required> 
                               <br> 
                     </ label> 
                    <label for="email"> 
                         Email:*    
                         <input type="text" id="email"  placeholder="Enter your email..." required>
                            <br> 
                    </label> 
                    <label for="password"> 
                        Password:*      
                        <input type="password" id="password" placeholder="Enter a password" required>
                            <br> 
                     </label> 
                     <label for="password2"> 
                        Reenter your    password:* 
                        <input  type="password" id="password2" placeholder="Reenter your password...">
                            <br> 
                     </label>           
                     <input type="button"  value="Submit"   onClick="myFunction()">
                     <script>   
                          function myFunction() {
                                var password = document.getElementById(password).value;     
                               var password2 =  document.getElementById(password    2).value;   
                           }
                           if (password.value !     = password2.value) {      
                                // do sth   alert("The passwords must   match"!); 
                            }  
                            }     
                       </script> 
                 </fieldset> 
            </form>
        </body> 
</html>

感谢您的回答!

-MoosMas

Javascript

var input_field_1 = document.getElementById("password");
var input_field_2 = document.getElementById("confirm_password");


function validate_passwords(){

  if(input_field_1.value == input_field_2.value){
    
    alert("Passwords matched !");
  
    // Your further processing with the form goes here...
  
  }
  
  else{
    alert("Passwords do not match !");
  };
  return false;
  
};
<form method="post" action="#">
    <input type="password" name="password" id="password">
    <input type="password" name="confirm_password" id="confirm_password">
    <button type="submit" onclick="validate_passwords()">Submit</button
</form>

现在我重新格式化了您的问题并揭示了您的 html 语法和 javascript 代码的格式错误的结构。将它们按如下顺序排列,可能会使它更好地工作。注意 input 和 br 元素末尾的 / 字符,javascript 上的 /label 元素位置和 } 的数量。在 javascript 上也使用 ' 而不是 " 并为脚本指定类型。onclick 也已更改。

希望对您有所帮助。

<!doctype html>
 <html>
  <head>
    <title>Register</title> 
  </head> 
  <body> 
    <form> 
       <fieldset> 
          <label for="uName"> Username:* </label> 
          <input type="text" id="uName" placeholder="Enter a username..." required /> 
          <br /> 
          <label for="email"> Email:* </label>
          <input type="text" id="email" placeholder="Enter your email..." required /> 
          <br />
          <label for="password"> Password:* </label>
          <input type="password" id="password" placeholder="Enter a password" required /> 
          <br />
          <label for="password2"> Reenter your password:* </label>
          <input type="password" id="password2" placeholder="Reenter your password..." /> 
          <br />
          <input type="button" value="Submit" onClick="javascript:myFunction();">
              <script type="text/javascript"> 
               function myFunction() { 
                   var password = document.getElementById('password').value;
                   var password2 = document.getElementById('password2').value; 
                   if (password != password2) {
                         // do sth 
                  alert('The passwords must match!');
                   } 
                } 
           </script> 
      </fieldset> 
   </form> 
 </body>