EasyUI Forum

General Category => General Discussion => Topic started by: catpaw on October 26, 2016, 04:17:17 PM



Title: [SOLVED] validate filebox
Post by: catpaw on October 26, 2016, 04:17:17 PM
hello,

Please help, I need validate the file size of a filebox

Code:
<input id="anexo" class="easyui-filebox" name="anexo" style="width:100%;height:32px;" data-options="
         buttonText: 'Seleccione un archivo',
            label: 'Anexo:',
            labelPosition: 'left',
            labelWidth: 100" />

This not work, because the input is not type file

Code:
alert($('#anexo').files[0].size);

how can I get the filebox size?

thanks


Title: Re: validate filebox
Post by: catpaw on October 26, 2016, 04:32:47 PM
hi

Its done  ;)

Code:
var f = $('#anexo').next().find('.textbox-value');
console.log(f[0].files);
alert(f[0].files[0].size);

thanks


Title: Re: validate filebox
Post by: catpaw on October 26, 2016, 04:44:47 PM
Please now I want to extend the validate with the size maximun

Code:
$.extend($.fn.validatebox.defaults.rules, {
maxSize: {
validator: function(){
var file = $(this).next().find('.textbox-value');
sizeKB = ((file[0].files[0].size)/1024).toFixed(0);
if(sizeKB > 999){ return false; }
},
message: 'The file must be 999 KB maximun.'
}
});

Code:
<input id="anexo" class="easyui-filebox" name="anexo" style="width:100%;height:32px;" data-options="
         buttonText: 'Seleccione un archivo',
            label: 'Anexo:',
            labelPosition: 'left',
            labelWidth: 100,
            validType:'maxSize'" />

but I get this error:

Quote
Uncaught TypeError: Cannot read property 'files' of undefined

What Im doing wrong?


Title: Re: validate filebox
Post by: catpaw on October 26, 2016, 04:51:49 PM
I change this:

var file = $(this).next().find('.textbox-value');

for this:

var file = $('#anexo').next().find('.textbox-value');

Code:
$.extend($.fn.validatebox.defaults.rules, {
maxSize: {
validator: function(value,param){
var file = $('#anexo').next().find('.textbox-value');
sizeKB = ((file[0].files[0].size)/1024).toFixed(0);
if(sizeKB > 999){ return false; }
},
message: 'The file must be 999 KB maximun.'
}
});

and its ok, it works

but how can I do it dynamic??

second:

I add a file of 425 KB and I get the maximun size error message

if(sizeKB > 999){ alert(sizeKB); return false; }

but 425 not is greater than 999 !!!


Title: Re: validate filebox
Post by: stworthy on October 26, 2016, 08:43:00 PM
Please try this code:
Code:
<script type="text/javascript">
$.extend($.fn.validatebox.defaults.rules, {
maxFileSize: {
validator: function(value, param){
var file = $(this).parent().find('.textbox-value');
var sizeKB = ((file[0].files[0].size)/1024).toFixed(0);
return sizeKB<=param[0];
},
message: 'The file must be less than {0}KB.'
}
});
$(function(){
$('#fb').filebox({
validType: "maxFileSize[99]"
});
})
</script>


Title: Re: validate filebox
Post by: catpaw on October 27, 2016, 09:05:33 AM
Perfect!!

thanks a lot