-2
I am trying to check the extension of a saved user submitted file in a variable receita
, but he will always bring me the error, for he is reading "receita"
and not what is inside the variable receita
.
Follow the code I’m using:
function run(receita){
var extensoes, ext, valido;
extensoes = new Array('.txt','.pdf','.doc','.png','.jpeg','.jpg');
ext = receita.substring(receita.lastIndexOf(".")).toLowerCase();
valido = false;
for(var i = 0; i <= receita.length; i++){
if(extensoes[i] == ext){
valido = true;
break;
}
}
if(valido){
return "Enviado com sucesso";
}
return "Extenção incorreta";
}
I tested it here and it worked normally
– Wictor Chaves
Here, regardless of which file I attach it always gives as "Incorrect extension"
– Maximiza Analyzer
I believe that the problem is time to use the function and not in the function, I will put two example for you to see what I’m talking about
– Wictor Chaves
Incutes the HTML as you are calling the function.
– Sam
Veja, is working normally https://jsfiddle.net/gnw31o20/
– Wictor Chaves
You put to return "Incorrect Extension" without the
else
.– Laércio Lopes
Wouldn’t it be right
if(valido){ return "Enviado com sucesso"; } else { return "Extenção incorreta"; }
– Laércio Lopes
@Laérciolopes doesn’t need the
else
because of thereturn
out of function.– Sam
@Sam that’s right, I had tested with console..
– Laércio Lopes
That one
for
also wrong and would only work by coincidence. See, if you add another extension in the array and calld.jpg
will return"Extenção incorreta"
because vc is based on string size and not array size.– Sam
Instead of
i <= receita.length
would have to bei <= extensoes.length
– Sam