2012-08-13 97 views
1

我想驗證我的表單使用jquery(字段'name1'是必需的,'email'也是必需的,並且應該以電子郵件格式。我跟着教程,但我無法得到它的工作我該如何解決jquery沒有驗證我的表格

預先感謝您

我的代碼:?

 <html> 
     <head> 
    <script src="/webvanta/js/jquery/1.4/jquery-1.4.2.min.js" type="text/javascript"></script> 
<script src="/webvanta/js/jquery/1.4/plugins/form/jquery.form.js" type="text/javascript"></script> 
<script src="/webvanta/js/jquery/1.4/plugins/validate/jquery.validate.min.js" type="text/javascript"></script> 
     </head> 
     <body> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
      $("#myform").validate({ 
      rules: { 
      name1: "required", 
      email: { 
       required: true, 
       email: true 
      } 
      }, 
      messages: { 
      name1: "Please specify your name", 
      email: { 
       required: "We need your email address to contact you", 
       email: "Your email address must be in the format of [email protected]" 
      } 
      } 
     }) 
     }); 

     </script> 

     <?php include 'header.php'; ?> 

     <form id = "myform" action="picture.php" method="get"> 
     Picture name: <input name="name1" class="required" /><br> 
     Email: <input name="email" class="required" /> 

     <input type="submit" value="Click" /> 
     </form> 
     <br> 




     </body> 
     </html> 
+3

考慮使用更新/最新版本的jQuery。 1.4.2約2.5歲。 – Tim 2012-08-13 11:02:01

+0

好的,感謝此通知 – fn27 2012-08-13 11:02:53

+0

其實這不是一個通知,這是你的代碼不工作的原因。我只是用最新的jQuery和最新版本的驗證插件檢查了代碼 - 它的工作原理。 – Tim 2012-08-13 11:10:48

回答

1

請一定要嘗試代碼使用庫的最新版本老圖書館老。插件可能包含大量的錯誤並且不符合標準

+0

絕對,我犯了多麼愚蠢的錯誤。謝謝@Tim – fn27 2012-08-13 11:17:39

1

首先,我從來沒有使用過這個函數,所以我不確定你提供的方法是否有效。但是,這不是jQuery文檔中描述的方式。作爲@Tim建議,首先切換到最新的jQuery版本,然後繼續前進,在此文檔頁面描述嘗試:

http://docs.jquery.com/Plugins/Validation#source

與此說,你還需要添加服務器端驗證藏漢,所以我只想繼續使用ajax和服務器端驗證(來自一個php文件或類似的)。然後,您將擁有完全相同的服務器端驗證和客戶端驗證方法。但是,如果你不使用數據庫層,服務器端驗證可能會過度。

2

一個非常簡單的Ajax例子:

$("#submit").click(function() { 

    var str = $("#your-form").serialize(); 


    $.ajax({ 
     type: "POST", 
     url: "url-to-your-file.php", 
     data: str, 
     success: function(msg) { 

      if(msg=="1") {//let's say 1 means error, do stuff here} 
      if(success=="0){//success, do stuff here} 
      } 
    }); 


    }); 

在你的PHP文件檢查和驗證輸入。如果沒有問題,則回顯0,否則回顯1 - 這只是一個例子,在這裏保持簡單。

+0

謝謝克里斯,我會考慮將來使用這個。 – fn27 2012-08-13 11:23:48