2015-02-07 115 views
-4

我正在编写注册表单,但我希望我的访问者输入两次密码。如果他们是一样的,没关系,但如果他们不是,他们会得到一个弹出窗口左右。该表单是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

+0

显示一些代码,如果你不知道如何 – Tarik 2015-02-07 20:04:43

回答

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>

+0

谢谢你,但它仍然不起作用,我把名字改为我使用的名字,但它仍然不起作用...感谢你的回答! – MoosMas 2015-02-07 21:16:05

+0

但是为什么它不起作用吗?代码片段工作正常... – 2015-02-07 22:36:28

0

现在我重新格式化你的问题,并透露你的HTML语法和JavaScript代码的畸形结构。把它们按照顺序排列,也许会使它工作得更好。注意输入和br元素末尾的/字符,/ javascript元素的位置和数量}。还可以使用“,而不是”对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> 

+0

http://jsfiddle.net/wwrnuf70/显示正确 – mico 2015-02-08 11:28:07

+0

获得您的合作后经过几次修复后,我确实了解你的收益。仍然想分享结果,因为它已经在我的屏幕上。 – mico 2015-02-08 11:30:11

相关问题