Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Validation for file extension is not checking properly in jquery

I have written a validation for excepting files which has extension of .xls and .xlsx So here is the code below.

function isFileValid() {
            var allowedFiles = [".xlsx", ".xls"];
            //  var allowedFiles = [".xls"];
            var fileUpload = document.getElementById("MainContent_fluploadData");
            var fileChanged = fileUpload.value !== window.lastUploadedFilename;
            window.lastUploadedFilename = fileUpload.value;
            var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$");

            if (!regex.test(fileUpload.value.toLowerCase()) && fileUpload.value !== '') {
                alert("Please upload files having extensions: " + allowedFiles.join(', ') + " only.");
                $('#MainContent_fluploadData').val('');
                return false;
            }
 }

So even if I am uploading valid excel file whose name is like :- SITE_ADDITION (1).xlsx

Please suggest.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you are looking for regEx please use the below regex for your code.

var regEx = new RegExp("^.*\.xls[xm]?$");//reg ex for excel file

console.log(regEx.test("abc.xlsx"));// true
console.log(regEx.test("abc.xls"));//true
console.log(regEx.test("abc.xlsm"));//true
console.log(regEx.test("abc.xlst"));//false
console.log(regEx.test("abc.doc"));//false
console.log(regEx.test("SITE_ADDITION (1).xlsx"));//true

about 4 years ago · Juan Pablo Isaza Relatório

0

If you are only concerned about the extension use this:

function isFileValid() {
    var allowedFiles = [".xlsx", ".xls"];
    var fileUpload = document.getElementById("MainContent_fluploadData");
    window.lastUploadedFilename = fileUpload.value;
    var regex = new RegExp('\\.(' + allowedFiles.join('|') + ')$', 'i');
    if (!regex.test(fileUpload.value)) {
        alert("Please upload files having extensions: " + allowedFiles.join(', ') + " only.");
        $('#MainContent_fluploadData').val('');
        return false;
    }
}

Notes:

  • you don't need to check for characters leading up to the . if you don't care about the chars in the file name, as long as you do not anchor the regex at the beginning.
  • '\\.(' - if you define a string and want to have a literal backslash you need to escape it: \\
    • in other words, string definition '\\.(' becomes \.( in memory
  • the second parameter to the RegExp constructor indicates case-insensitivity

If on the other hand you want to restrict the characters in the filename, use this:

function isFileValid() {
    var allowedFiles = [".xlsx", ".xls"];
    var fileUpload = document.getElementById("MainContent_fluploadData");
    window.lastUploadedFilename = fileUpload.value;
    var regex = new RegExp('^[a-z0-9\\s_\\.\\-:\\(\\)]+\\.(' + allowedFiles.join('|') + ')$', 'i');
    if (!regex.test(fileUpload.value)) {
        alert("Please upload file with only A-Z, 0-9, space, dot, dash, :, () in name, and extension: " + allowedFiles.join(', '));
        $('#MainContent_fluploadData').val('');
        return false;
    }
}

Notes:

  • the regex needs to be anchored at the beginning with ^
  • tweak the character class as needed for allowed chars
  • on the other hand, in case you have to full path name in your filename, start the regex string with: '[\\/\\\\][a-z0-9...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda