[英]Validate file size and type in a multiple input
i have 3 input file, i wanna make a javascript validation for 3 input file in one submit button form event onSubmit="" 我有3个输入文件,我想在一个提交按钮表单事件onSubmit =“”中对3个输入文件进行JavaScript验证
<form action="step2_crud_dev.php" method="post" enctype="multipart/form-data" class="form-horizontal" role="form" id="dataPribadi" >
<div>
<input type="file" name="fUpload1" id="fileUpload1"/>
<input type="file" name="fUpload2" id="fileUpload2"/>
<input type="file" name="fUpload3" id="fileUpload3"/>
</div>
<div>
<input type="submit" name="upload" value="upload" />
</div>
</form>
EDITED CODE new code that working but, still proccess saving EDITED CODE新代码可以正常工作,但仍可以保存
$(document).ready(function(){
$('#tbl_next').click(function(){
//alert('hello');
$('input[type="file"]').each(function(){
var thisFile = $(this);
var fileSize = thisFile[0].files[0].size;
var fileType = thisFile[0].files[0].type;
//alert(fileSize);
if(fileSize>1048576){ //do something if file size more than 1 mb (1048576)
alert(fileSize +" bites\n ukuran gambar terlalu besar");
return false;
}else{
switch(fileType){
case 'image/png':
case 'image/gif':
case 'image/jpeg':
case 'image/pjpeg':
alert("Acceptable image file!");
break;
default:
alert('Unsupported File!');
return false;
}
}
});
$('form#dataPribadi').submit();
});
});
Change type of submit button to normal button and use onclick
handler for it by giving a id
as upload
, 将提交按钮的类型更改为普通按钮,并通过提供
id
作为upload
为其使用onclick
处理程序,
<form action="step2_crud_dev.php" method="post" enctype="multipart/form-data" class="form-horizontal" role="form" id="dataPribadi" >
<div>
<input type="file" name="fUpload1" id="fileUpload1"/>
<input type="file" name="fUpload2" id="fileUpload2"/>
<input type="file" name="fUpload3" id="fileUpload3"/>
</div>
<div>
<input type="button" id="upload" value="upload" />
</div>
</form>
and your click event handler should be, 您的点击事件处理程序应该是
$(document).ready(function(){
$('#upload').click(function(){
alert('hello');
$('input[type="file"]').each(function(){
var thisFile = $(this);
var fileSize = thisFile[0].files[0].size;
alert(fileSize);
});
$('form#dataPribadi').submit();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.