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-9s_\.-:])+(" + 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.
3
Answers
If you are looking for regEx please use the below regex for your code.
If you are only concerned about the extension use this:
Notes:
.
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:\
'\.('
becomes.(
in memoryRegExp
constructor indicates case-insensitivityIf on the other hand you want to restrict the characters in the filename, use this:
Notes:
^
'[\/\\][a-z0-9...