Problem validating file field

Asked

Viewed 27 times

1

I took a Function here from the Internet to try to validate my field 'file', which will be for the client to send photos, I want the client can not leave the field 'file' empty, without any upload, and I want it to be at most 300mb, and that is in the format or jpeg or jpg or png. I only got a code to validate if it’s over 300MB, I just don’t know if you’re totally right, this:

function validafoto() {
var upload = document.cad.getElementById("upload");
    var size = upload.files[0].size;
    if(size > 3145728) { //3MB         

      alert('Não permitido'); //Acima do limite
     return false;

}

And now I don’t know how to call it in my form, I tried it in my validarformular Function, but it didn’t work out very well, could it help? I’ll leave the entire code of my form here: https://docs.google.com/document/d/e/2PACX-1vSFeShPqOtM5dO6_swPcERh52AQyMS3TdMo2fgP_aRvyj4TslepSfQq1I8yUwglb3tvkrJxQ1cwGD1-/pub

  • A jpeg of 300mb? It’s a photo of Hubble? rs

  • 1

    @dvd 300mb=3MB, each 100mb vale 1MB capisce

  • @Leocaracciolo knew not to

1 answer

1


The function is correct in the part of checking the file weight, there is only one error in this method: document.cad.getElementById("upload");, where cad is the id form and does not fit in this way in the method. It would have to be document.getElementById("upload");.

To know if the field is empty, you can use this if before catching the size aqruivo:

if(!upload.value){
   alert('Selecione um arquivo');
   return false;
}

To check the type (extension), it would be this if:

var tipo = upload.value.split(".").pop();          // pego a extensão do arquivo
var valida_tipo = tipo.match(/^(jpeg|jpg|png)$/i); // verifico se é jpeg, jpg ou png
if(!valida_tipo){
   alert('Tipo não permitido');
   return false;
}

The full function would look like this:

function validafoto() {
   var upload = document.getElementById("upload");
   if(!upload.value){
      alert('Selecione um arquivo');
      return false;
   }

   var tipo = upload.value.split(".").pop();
   var valida_tipo = tipo.match(/^(jpeg|jpg|png)$/i);
   if(!valida_tipo){
      alert('Tipo não permitido');
      return false;
   }

   var size = upload.files[0].size;
   if(size > 3145728) { //3MB         
      alert('Não permitido'); //Acima do limite
      return false;
   }

   return true;
}

To validate from another function, simply call if(!validafoto()) return;.

  • i put the function in my code but still not working even without uploading the file it still sends the form

  • You have to call the function of the photo in the form function. I hope I will pass you the code.

  • I can use the onchange?

  • In function validarFormulario(), include the line if(!validafoto()) return;

  • The onchange would not look good, have to validate in the form function same.

  • Thank you again, @dvd

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.