javascript多输入文件验证

时间:2017-01-27 05:05:48

标签: jquery validation

我从网站http://www.allphptricks.com/check-file-size-extension-uploading-using-jquery/

获取脚本

HTML

<form name="form" method="post" action="" enctype="multipart/form-data" >
<p>
<label>Upload Picture:</label>
<input type="file" name="file" id="picture" required />
</p>
<p id="error1" style="display:none; color:#FF0000;">
Invalid Image Format! Image Format Must Be JPG, JPEG, PNG or GIF.
</p>
<p id="error2" style="display:none; color:#FF0000;">
Maximum File Size Limit is 1MB.
</p>
<p>
<input name="submit" type="submit" value="Submit" id="submit" />
</p>
</form>

脚本

$('input[type="submit"]').prop("disabled", true);
var a=0;
//binds to onchange event of your input field
$('#picture').bind('change', function() {
if ($('input:submit').attr('disabled',false)){
    $('input:submit').attr('disabled',true);
    }
var ext = $('#picture').val().split('.').pop().toLowerCase();
if ($.inArray(ext, ['gif','png','jpg','jpeg']) == -1){
    $('#error1').slideDown("slow");
    $('#error2').slideUp("slow");
    a=0;
    }else{
    var picsize = (this.files[0].size);
    if (picsize > 1000000){
    $('#error2').slideDown("slow");
    a=0;
    }else{
    a=1;
    $('#error2').slideUp("slow");
    }
    $('#error1').slideUp("slow");
    if (a==1){
        $('input:submit').attr('disabled',false);
        }
}
});

运作良好。但我有多个文件数组字段。

<input type="file" name="file[]" id="picture" required />
<input type="file" name="file[]" id="picture" required />

那么如何将所有字段验证为同名数组。

0 个答案:

没有答案