2014-11-14 41 views
0

我正在使用jquery驗證插件來驗證表單。如果所有字段都保留空白,它正在工作。但是,如果我在輸入字段中輸入了一個文件並留空了其他必填字段,則不會再驗證其他元素,而只是將表單提交給服務器。jquery驗證插件不與文件一起工作

<script> 
$().ready(function() { 
    $("#form1").validate({ 
     submitHandler: function (form) { 
      $.ajax({ 
       type: $(form).attr('method'), 
       url: $(form).attr('action'), 
       data: $(form).serialize(), 
      }) 
      .done(function (response) { 
       jAlert(response);  
      }); 
      return false; 
     }, 
     ignore: [], 
     rules: { 
      title: { required: true, minlength: 2, maxlength: 25 }, 
      text1: { 
       required: function() { 
        CKEDITOR.instances.text1.updateElement(); 
       } 
      }, 
      text2: { 
       required: function() { 
        CKEDITOR.instances.text2.updateElement(); 
       } 
      }, 
      newsimage: { 
       required: true, 
       accept: "image/*" 
      }, 
     }, 
     messages: { 
      title: { 
       required: "Please enter the Title", 
       minlength: "Title must consist of at least 2 characters" 
      }, 
      text1: { 
       required: "Please enter text", 
       minlength: "Must consist of at least 2 characters" 
      }, 
      text2: { 
       required: "Please enter text", 
       minlength: "Must consist of at least 2 characters" 
      } 
     } 
    }); 
}); 
</script> 

這裏的形式

<form id="Form1" enctype="multipart/form-data" method="post" action="save.php" > 

    Title: <input name="title" size="40" maxlength="255"> 
    <br> 
    <label for="newsimage">News Image</label> 
    <input type="file" id="newsimage" name="newsimage"> 

    <br> News Summary: 
    <textarea id="text1" name="text1" rows="7" cols="30"></textarea> 

    <script type="text/javascript"> 
      CKEDITOR.replace('text1'); 
    </script> 
    <br> News Full Story: 
    <textarea id="text2" name="text2" rows="7" cols="30"></textarea> 
    <script type="text/javascript"> 
      CKEDITOR.replace('text2'); 
    </script> 
    <br> <input type="submit" name="submit" value="Add News"> 

+0

似乎罰款http://jsfiddle.net/arunpjohny/hwfkn0v7/2/ - 你需要從'required'返回TRUE;回調方法 –

+0

我的代碼有問題,它不工作,刪除ckeditor,甚至複製你放在jsfiddle中的東西,但它仍然是同樣的問題。 –

回答

0

你可以使用需要你的表單標籤是這樣的:

<input name="title" size="40" maxlength="255" required> 

應該迫使一些輸入。 (它沒有被消毒)

0

這不是我的最終目標,因爲我缺少additional-methods.js,因此添加它可以解決問題。

<script src="js/additional-methods.min.js"></script> 
相關問題