验证文件大小并输入多个输入 [英] Validate file size and type in a multiple input

查看:88
本文介绍了验证文件大小并输入多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个输入文件,我想在一个提交按钮表单事件中对3个输入文件进行javascript验证onSubmit =

i have 3 input file, i wanna make a javascript validation for 3 input file in one submit button form event onSubmit=""

<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

$(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();
    });
});


推荐答案

将提交按钮的类型更改为普通按钮并使用 onclick 处理程序,给出 id as upload

Change type of submit button to normal button and use onclick handler for it by giving a id as upload,

<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();
    });
});

UPDATED FIDDLE

这篇关于验证文件大小并输入多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆