how to write a validate function for reading only excel file in xls and xlsx format

how to write a validate function for reading only excel file in xls and xlsx format

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.

Solutions/Answers:

Solution 1:

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

References

Related:  Bash: cutting a delimited fragment of each string [closed]