我使用extension
将以下规则添加到现有的jQuery Validate脚本中,但是此片段会导致它破坏而根本不进行验证。我之前和之后尝试了所有可能的逗号组合,并且我也尝试使用mimetypes的accept
方法,但到目前为止还没有任何工作。如果我注释掉这个片段(以及它之前的行上的注释),代码就可以正常工作,但添加它会导致它破坏。
Here's a jsFiddle代码按原样(不起作用),here's one代码完全相同,但代码段已被注释掉(工作正常)。
摘录本身:
form1upload: {
extension: "jpg|jpeg|pdf|doc|docx|png"
}
完整代码:
jQuery(document).ready(function() {
jQuery("#grantapp").validate({
errorClass:"errorlabels",
rules: {
form1name: "required",
form1building: "required",
form1position: "required",
form1phonex: "required",
form1besttime: "required",
form1projtitle: "required",
form1benefit: "required",
form1timeframe: "required",
form1relevance: "required",
form1description: "required",
form1amount: "required",
form1acceptchk: "required",
form1upload: {
extension: "jpg|jpeg|pdf|doc|docx|png"
}
},
messages: {
form1name: "You must enter your name.",
form1building: "You must enter your building.",
form1position: "You must enter your position.",
form1phonex: "You must enter your phone extension.",
form1besttime: "You must enter the best time to contact you.",
form1projtitle: "You must give your project a title.",
form1benefit: "You must enter the number of students who will benefit.",
form1timeframe: "You must enter a time frame for this project.",
form1relevance: "<br />You must state how this project is relevant to education.",
form1description: "<br />You must provide a description of your project.",
form1amount: "You must enter a requested amount.",
form1acceptchk: "<span style=\"position:relative;top:-10px;\">You must accept the terms.</span>",
form1upload: "You may not upload this type of file."
},
errorPlacement: function (error, element) {
if (element.attr("name") == "form1acceptchk") {
error.insertAfter("#tbl1");
} else {
error.insertAfter(element);
}
}
});
});
答案 0 :(得分:17)
这是因为默认情况下the extension
rule/method不是jQuery Validate插件的一部分。
如果要使用extension
规则,则必须包含jQuery Validate插件的additional-methods.js
file。
现在似乎正在运作......