2015-09-09 94 views
0

我有一個使用jQuery驗證插件的窗體的代碼設置(我刪除了不必要的代碼)。什麼擴展與jQuery驗證擴展功能一起工作?

$("#form").validate({ 
    rules: { 
     attachment: {extension: "txt|rtf|jpg|jpeg|png|pdf|doc|docx|xls|xlsx|ppt|pptx|bmp|tif"} 
    }, 
    submitHandler: function(form) { 
     return false; 
    }, 
    showErrors: function(errorMap, errorList) { 
     if(this.numberOfInvalids() > 0 && errorList.length > 0) { 
      if($(errorList[0].element).attr("id") == "attachment") { 
       displayModStatusMessage(false, errorList[0].message, $("#modResponse"), false, 505); 
      } 
      else { displayModStatusMessage(false, errorList[0].message, $("#modResponse"), false); } 
     } 
     else if(this.numberOfInvalids() == 0) { removeMessage($("#modResponse")); } 
    } 
}); 

由於某些原因,如果我去嘗試添加帶有.txt結尾或.doc結尾的附件,則會顯示錯誤消息。它正在使用.png。

這裏是重要的HTML

<tr> 
    <td class="textright"><label for="attachment">Attach:</label></td> 
    <td class="textleft"><input type="file" id="attachment" name="attachment" value="" accept=".txt, .rtf, .jpg, .jpeg, .png, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .bmp, .tif" /></td> 
</tr> 

我要補充一點,我使用jQuery的最新版本驗證(1.14.0)和最新版本的其他方法(1.14.0)。

+0

顯示相關的HTML。 – Sparky

+0

@sparky對不起,當我嘗試做一個.png錯誤信息不顯示,這是正確的。 – Metropolis

+0

您在'rules'對象後面缺少一個逗號。除此之外,我無法在jsFiddle中重現您的問題。 – Sparky

回答

1

您在HTML中具有accept屬性。這被拾取並由jQuery Validate插件的相應accept method使用。

<input accept=".txt, .rtf, .jpg, .jpeg, .png, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .bmp, .tif" ... /> 

通過使用它,你有效地應用這兩個acceptextension方法來驗證文件擴展名。但是,在jQuery Validate中,accept方法僅用於驗證MIME類型。

完全刪除accept屬性。