4
I have a code that checks the size before sending the file, I would like to know how to check the file extension together, for example, I need these extensions .jpg, png, .gif, .pdf, .txt, .doc, .docx
. Could I do it in the same code? How would I do it?
Code
$j(function () {
$j("#arquivo").change(function () {//ou Id do input
var fileInput = $j(this);
var maxSize = $j(this).data('max-size');
console.log(fileInput.get(0).files[0].size);
//aqui a sua função normal
if (fileInput.get(0).files.length) {
var fileSize = fileInput.get(0).files[0].size; // in bytes
if (fileSize > maxSize) {
$j('#resultado').text('Arquivo excedeu o limite permitido, por favor escolha arquivos com no maximo 2GB*');
$j('#validate').attr('disabled', 'disabled');
} else {
$j('#validate').removeAttr('disabled');
$j('#resultado').hide();
}
}
});
});
I tried this, but it error to any extension. It displays the message even if the extension is png, jpg, etc..
– Gustavo Souza
@Gustavosouza Sorry Gustavo, there was a glitch in the if, test now please
– Diego Marques
It’s not working yet, if you want to take a test there to see
– Gustavo Souza
I set the name of its variable within the function and isolated the text, updated response ;)
– Diego Marques
I couldn’t do it by merging the 2 in 1 only, but I used your example separately and do what I need, it would be nice if the two of you worked together, but it’s functional, thank you.
– Gustavo Souza
There is another question, if I put a file and it is invalid, when I remove it and do not put another in place it continues with the visible error, would have to put if there is no file it "take out" the error?
– Gustavo Souza
at the event
change
, you can put an IF to check if the input field value is empty, if it is, you give an Hidden display or something like that in your msg– Diego Marques
Thank you for the code, it worked perfectly. Thank you
– Betini O. Heleno