On file upload change event JavaScript function not firing
<asp:FileUpload ID="fudigitalfile" accept=".pdf,.PDF" runat="server" onchange="Filevalidation()" />
<script>
function Filevalidation() {
lblError.innerHTML = "";
var allowedFiles = [".doc", ".docx", ".pdf"];
var fileUpload = document.getElementById("<%=fudigitalfile.ClientID %>");
var lblError = document.getElementById("lblError");
var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$");
if (!regex.test(fileUpload.value.toLowerCase())) {
lblError.innerHTML = "Please upload files having extensions: <b>" + allowedFiles.join(', ') + "</b> only.";
return false;
}
else {
alert("File selected");
}
//check size
const fi = document.getElementById("<%=fudigitalfile.ClientID %>");
// Check if any file is selected.
if (fi.files.length > 0) {
for (const i = 0; i <= fi.files.length - 1; i++) {
const fsize = fi.files.item(i).size;
const file = Math.round((fsize / 1024));
// The size of the file.
if (file >= 4096) {
alert(
"File too Big, please select a file less than 4mb");
} else {
document.getElementById('size').innerHTML = '<b>'
+ file + '</b> KB Size File Selected ';
}
}
}
}
</script>