简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM