Number of Uploaded files function does not show the error message

Viewed 69

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:

enter image description here

and this is what I see when I inspect the element:

enter image description here

uploadcerts is the name of my page.

Any help will be greatly appreciated.

1 Answers

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" />
Related