locked
Number of Uploaded files function does not show the error message RRS feed

  • Question

  • User351619809 posted

    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.

    Any help will be greatly appreciated.

    Monday, September 14, 2020 12:28 AM

Answers

  • User-939850651 posted

    Hi anjaliagarwal5,

    I created a simple example using the code you provided, and I modified part of the code to fix your problem.

    Please refer to the following code:

    <head runat="server">
        <title></title>
        <script> 
            function ValidateFile2(sender, args) {
                var fileCount = document.getElementById('UploadPDF').files.length;
                console.log(fileCount)
                if (fileCount > 2) {
                    args.IsValid = false;
                }
                else if (fileCount <= 0) {
                    args.IsValid = false;
                } else {
                    args.IsValid = true;
                }
            }
        </script>
    </head>
    <body>
        <form id="Form1" runat="server">
            <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 ID="customValidatorUpload"
                Display="Static" runat="server"
                ErrorMessage="Only five files can be uploaded"
                ControlToValidate="UploadPDF"
                ClientValidationFunction="ValidateFile2" />
        </form>
    </body>

    Result:

    1. The value of the ClientValidationFunction attribute is just the function name.
    2. In your code, args.IsValid is always true in the function.

    Please check them.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2020 5:40 AM