I am trying to limit the number of files that can be uploaded to 2. I have the following validator and code to resolve this issue:
<asp:ValidationSummary runat="server" ID="ValidationSummary1"
DisplayMode="BulletList" ShowMessageBox="false" ShowSummary="True" CssClass="alert alert-danger" />
<asp:FileUpload runat="server" ID="UploadPDF" AllowMultiple="true" accept=".pdf" />
<asp:CustomValidator Display="None" ID="customValidatorUpload" runat="server" ErrorMessage="Only five files can be uploaded" ControlToValidate="UploadPDF" ClientValidationFunction="ValidateFile2();" />
Below is the ValidateFile2() function:
<script>
function ValidateFile2(sender, args) {
var fileCount = document.getElementById('UploadPDF').files.length;
if (fileCount > 2)
{
args.IsValid = false;
}
else if (fileCount <= 0)
{
args.IsValid = false;
}
args.IsValid = true;
}
</script>
When I try to upload more than 2 files, I dont see any error message. I am not sure what am I doing wrong.
I tried to put
console.log(fileCount);
inside ValidateFile2 function and this is the error that I see in under console in the browser:
and this is what I see when I inspect the element:
uploadcerts is the name of my page.
Any help will be greatly appreciated.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This is what I did to resolve the issue. I removed the parentheses from ValidateFile2 function and code started working right away. Below is the updated code:
<asp:ValidationSummary runat="server" ID="ValidationSummary1"
DisplayMode="BulletList" ShowMessageBox="false" ShowSummary="True" CssClass="alert alert-danger" />
<asp:FileUpload runat="server" ID="UploadPDF" AllowMultiple="true" accept=".pdf" />
<asp:CustomValidator Display="None" ID="customValidatorUpload" runat="server" ErrorMessage="Only five files can be uploaded" ControlToValidate="UploadPDF" ClientValidationFunction="ValidateFile2" />
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

