Check a file extension

Asked

Viewed 92 times

-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

  • Here, regardless of which file I attach it always gives as "Incorrect extension"

  • 1

    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

  • Incutes the HTML as you are calling the function.

  • Veja, is working normally https://jsfiddle.net/gnw31o20/

  • You put to return "Incorrect Extension" without the else.

  • Wouldn’t it be right if(valido){ return "Enviado com sucesso"; } else { return "Extenção incorreta"; }

  • 1

    @Laérciolopes doesn’t need the else because of the return out of function.

  • @Sam that’s right, I had tested with console..

  • 1

    That one for also wrong and would only work by coincidence. See, if you add another extension in the array and call d.jpg will return "Extenção incorreta" because vc is based on string size and not array size.

  • Instead of i <= receita.length would have to be i <= extensoes.length

Show 6 more comments

2 answers

1

Today there is a more current and functional alternative to find out if a given value belongs to an array. You can use the method Array.prototype.includes() of ES7.

function run() {

    var receita = document.getElementById("receita").value;
    var ext, valido;
    
    ext = receita.substring(receita.lastIndexOf(".")).toLowerCase();

    valido = ['.txt', '.pdf', '.doc', '.png', '.jpeg', '.jpg'].includes(ext);

    if (valido) {
        alert("Extensão válida!");
    } else {
        alert("Extensão incorreta.");
    }                

}
<!DOCTYPE html>
<html>
    <head>    
        <meta charset="UTF-8"/>
    </head>
    <body>
        <input type="text" name="receita" id="receita" size=50 placeholder="Nome do arquivo">
        <button onclick="run();">Enviar</button>
    </body>
</html>

However the method, includes() is not supported in older browsers, this may be a problem if you do not use some transpilator to make the conversion from ES7 to ES5.

Compatibility (can I use) Array.prototype.indexOf:

inserir a descrição da imagem aqui

Compatibility (can I use) Array.prototype.includes:

inserir a descrição da imagem aqui

Reference: https://www.w3schools.com/jsref/jsref_includes.asp

-2

function run() {

    var receita = document.getElementById("receita").value;
    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) {
        alert("Extensão válida!");
    } else {
        alert("Extensão incorreta.");
    }                

}
<!DOCTYPE html>
<html>
    <head>    
        <meta charset="UTF-8"/>
    </head>
    <body>
        <input type="text" name="receita" id="receita" size=50 placeholder="Nome do arquivo">
        <button onclick="run();">Enviar</button>
    </body>
</html>

  • Your code is incorrect. Put d.jpg in the file name and you will see.

  • Still the same problem. The problem is in the else, because it will enter it at the first extension that does not match the array item.

  • I changed here already.

Browser other questions tagged

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