哪些扩展与 jQuery 验证扩展功能一起使用?

What extensions work with jQuery validate extension function?

我使用 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)。

您在 HTML 中拥有 accept 属性。这由 jQuery 验证插件的相应 accept method.

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

通过使用它,您可以有效地应用 acceptextension 方法来验证文件扩展名。但是,在 jQuery Validate 中,accept 方法仅用于验证 MIME 类型。

完全删除 accept 属性。