3

How can i write a function to validate if file uploaded is not in excel file format ".xls or .xlsx" it will prompt error message. Any help would be appreciated.

What i've tried so far. Below function will prompt error message too if read excel file in .xlsx format .

function verifyFile()
{

       if(document.mainform.ATTACH_FILE.value.search(/\.(xls)$/) == -1) {
        alert("Invalid filename extension!");
        return false;
    }
}

Expected result : accept both .xls and .xlsx format, prompt messsage if other than these format.

levi
  • 22,001
  • 7
  • 73
  • 74
user3835327
  • 1,194
  • 1
  • 14
  • 44

1 Answers1

3

You dont need regex to do that.

   var extension = document.mainform.ATTACH_FILE.split(".").pop();

   if (['xls','xlsx'].indexOf(extension) < 0) { 
        alert("Invalid filename extension!");
        return false;
   }

Check other examples in this answer

Community
  • 1
  • 1
levi
  • 22,001
  • 7
  • 73
  • 74