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

时间:2015-02-07 20:02:11

标签: javascript html forms passwords

我正在编写注册表单,但我希望我的访问者输入两次密码。如果它们是相同的,那就没关系,但如果它们不是,它们会弹出一个左右。表单是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

2 个答案:

答案 0 :(得分:0)

<强>的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>

答案 1 :(得分:0)

现在我重新格式化了你的问题,并揭示了你的html语法和javascript代码的格式错误。把这些按顺序排列,可能会让它更好用。注意javascript上输入和br元素/ /标签元素位置和数量}的/字符。也可以在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>