2013-10-26 115 views
0

我剛剛閱讀jQuery驗證,並遇到了問題。點擊提交按鈕不會觸發任何驗證。我一直在尋找幾個小時,但不幸的是,我找不到解決方案。我的jQuery驗證不工作,因爲它應該

我的代碼:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
     <title> Test </title> 

     <style> 

       body { 
         max-width: 70em; 
         margin: auto; 
         padding: 0; 
       } 

       .container { 
         max-width: 50%; 
         margin: auto auto; 
         -webkit-border-radius: 5px; 
         -moz-border-radius: 5px; 
         border-radius: 5px; 
       } 

       input, button { 
         display: block; 
         margin: 0.5em auto; 
         height: 2em; 
         width: 90%; 
       } 

       button { 
         border: 0; 
         background-color: green; 
       } 

       .text-center { 
         text-align: center; 
       } 
     </style> 

</head> 

<body> 

     <div class="container"> 
       <form method="POST" id="form"> 
         <h2 class="text-center"> Register! </h2> 
         <label> Username </label> 
         <input type="text" name="username"> 
         <label> Password </label> 
         <input type="password" name="password"> 
         <label> Password Confirmation </label> 
         <input type="password" name="password_confirmation"> 
         <label> Email </label> 
         <input type="password" name="password_confirmation"> 
         <input type="submit" value="Submit"> 
       </form> 
     </div> 

     <script src="http://code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script> 
     <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.js" type="text/javascript"></script> 

     <script> 

     $(document).ready(function() { 
       $("#form").validate({ 
         rules: { 
           username: { 
             required: true, 
             range: [4, 15] 
           } 

           password: { 
             required: true, 
             minlength: 8 
           } 

           email: { 
             required: true, 
             email: true 
           } 
         } 
       }); 
     }); 



     </script> 
</body> 
</html> 

的jsfiddle:http://jsfiddle.net/hPmec/

+0

你每個對象後忘了逗號... – Terry

+0

你應該看過Java由於缺少逗號而導致控制檯日誌中的腳本錯誤。 – Sparky

回答

3

你必須在腳本語法錯誤

$(document).ready(function() { 
    $("#form").validate({ 
     rules: { 
      username: { 
       required: true, 
       range: [4, 15] 
      }, //missing , 

      password: { 
       required: true, 
       minlength: 8 
      }, //missing , 

      email: { 
       required: true, 
       email: true 
      } 
     } 
    }); 
}); 

演示:Fiddle

相關問題