我有一个带有输入字段的HTML表单来上传文件。我希望选择的文件与所需的正确文件名匹配(mcust.csv)。如果他们选择了不同的文件,我想用JS触发错误。
表格如下所示:
<form name="upload-form" id="upload-form" action="upload-information.php" enctype="multipart/form-data" method="post">
<input type="file" name="dealers_csv" id="dealers_csv" required="required" data-message="Please attach the mcust.csv file.">
<button type=\"submit\" name=\"Submit\" id=\"Submit\" class=\"csbutton-color upload-button\" style=\"margin-right:10px;\" >Submit files</button><img id=\"loader\" src=\"../images/loading.gif\" style=\"display:none;\"/>
</form>
用户附加文件后,我想检测文件的名称( mcust.csv ),如果文件名不是 mcust.csv ,我想要在提交表单之前使用Javascript / jQuery“做某事”(显示错误)。
这是JS:
$(document).ready(function () {
$("#upload-form").validator({
position: 'top center',
offset: [-12, 40],
relative: true,
accept: "csv",
message: '<div><em></em></div>'
})
// make sure that mcust.csv is the attached file. If not, throw alert message
$("input#dealers_csv").on("change", function() {
var dealers = $("input#dealers_csv");
var arrfilepath = dealers.val().split("\\");
var filename = arrfilepath[arrfilepath.length - 1];
if (filename != "mcust.csv") {
alert("Wrong file! Please select 'mcust.csv'");
dealers.val('');
}
});
// make sure that morders.csv is the attached file. If not, throw alert message
$("input#orders_csv").on("change", function() {
var orders = $("input#orders_csv");
var arrfilepath2 = orders.val().split("\\");
var filename2 = arrfilepath2[arrfilepath2.length - 1];
if (filename2 != "morders.csv") {
alert("Wrong file! Please select 'morders.csv'");
orders.val('');
}
});
});
编辑:我已将上述代码更新为工作版本。一个问题:如果他们附加了错误的文件,是否有办法显示数据消息而不是“警报”?
答案 0 :(得分:2)
如果数据正常,请通过JavaScript提交表单。
<script>
function validate(){
if (document.getElementById('dealers_csv').value != 'mcust.csv'){
alert('Filename must be mcust.csv');
}else{
document.getElementById('upload-form').submit();
}
}
</script>
<form name="upload-form" id="upload-form" enctype="multipart/form-data" method="post">
<input type="file" name="dealers_csv" id="dealers_csv">
<input type="button" value="Submit" onclick="validate()"/>
</form>
答案 1 :(得分:1)
这:http://jsfiddle.net/pratik136/DpJYe/ 应该回答你的问题
编辑:小提琴更新以反映您的表单提交要求
答案 2 :(得分:1)
这已在这里得到解答
https://stackoverflow.com/a/651767/437226
这将检查扩展名。
要在没有过多代码的情况下检查文件名,请输入隐藏字段:
<input id="attachmentname" type="hidden" value="mcust.csv" />
然后在规则中添加等效检查器:
$("#myform").validate({
rules: {
attachment: {
equalTo: "#attachmentname"
}
}
});
答案 3 :(得分:1)
我认为你正在使用JQuery工具验证器。以下是经过修改的验证代码。
HTML
<form name="upload-form" id="upload-form" action="upload-information.php" enctype="multipart/form-data" method="post">
<input type="file" name="dealers_csv" id="dealers_csv" required="required" filename="mcust.csv">
<button type="submit" name="Submit" id="Submit" class="csbutton-color upload-button" style="margin-right:10px;" >Submit files</button><img id="loader" src="../images/loading.gif" style="display:none;" />
</form>
的Javascript
$(document).ready(function () {
//definfing custom validator
$.tools.validator.fn("[filename]", function(input, value) {
var fName = input.attr("filename"),
regX = new RegExp('.*\\' + fName + '$');
if (value.match(regX)) {
return true;
} else {
return {
en : "Wrong file! Please select '" + fName + "'"
};
}
});
// assigning validator to file input
$("#dealers_csv").validator({
position: 'top center',
offset: [-12, 40],
relative: true,
message: '<div><em></em></div>'
});
//triggering validation on change of file
$("#dealers_csv").change(function(){
$(this).data("validator").checkValidity();
});
});
尝试理解此代码并根据需要进行修改
答案 4 :(得分:0)
将您的更改事件处理程序修改为:
$(document).ready(function () {
$("#dealers_csv").change(function(){
var dealers =$(this).val();
if(!dealers.match(/.*\\mcust.csv$/g)){
alert("Wrong file!" + dealers);
this.value = "";
return false;
}
});
});