我有一个带有文件上传功能的 html 表单,我想使用 jquery 插件进行验证。现在,如果文件为空,那么它会显示一条错误消息,但如果文件的扩展名不同,那么它应该显示消息“文件类型不允许”,但它似乎没有传递到文件< strong>filecheck.php 页面将验证文件类型。
如何使用此 jquery 插件验证此文件类型?你能帮我吗?谢谢。
HTML 部分:
<div id="result"></div> <!--error or success message will be show here-->
<form action="editContactDetails.php" method="post" enctype="multipart/form-data" id="all_contact_details">
<tr>
<td></td>
<td align="left"><input name="file1" type="file" id="file" class="file"/></td>
<td></td>
</tr>
</form>
Jquery 部分:
$(document).ready(function() {
// validate signup form on keyup and submit
$("#all_contact_details").validate({
submitHandler: function(form) {
$.ajax({
url: form.action,
type: form.method,
//async: false,
data: $(form).serialize(),
beforeSend : function (){
$('input[type=submit]').attr('disabled', false);
},
success: function(response) {
$('#result').html(response);
//$(form).find('div').hide();
$(form).hide();
}
});
},
rules: {
file1: {
required: true,
remote: {
url: "filecheck.php",
type: "post"
}
},
},
messages: {
file1: {
required: "Upload your file",
remote: "File is not allowed"
},
}
});
});
Php 部分(filecheck.php)
<?php
$file1 = $_FILES['file1']['name'];
$allowedExts = array("jpg");
$temp = stripslashes($file1);
$temp = explode(".", $temp);
$extension = end($temp);
if(!in_array($extension, $allowedExts))
echo 'false';
else
echo 'true';
?>
最佳答案
SubmitHandler
在表单有效时处理实际提交:
http://jqueryvalidation.org/validate/#submithandler
所以这不会验证文件扩展名本身。
但是,您可以使用 jqueryvalidation 中内置的 extension
验证器,它将完全实现您想要实现的目标。请参阅此链接以获取引用和示例:
关于javascript - 输入类型=文件未使用 Jquery 验证插件传递到 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24076721/