Set ASP.Net FileUpload control limitation using CustomValidator

payalsh
 
on Jan 29, 2022 06:47 AM
692 Views

I have file upload control of asp.net in which i have enable multifile upload. But now i want to apply limitations so that user can uplaod only 4 files not more than that.

for this i have use below coding but it dose'nt work now - 

Please advice me 

<%@ Page Title="" Language="C#" MasterPageFile="../Builder/Builder.master" AutoEventWireup="true" CodeFile="post.aspx.cs" Inherits="Builder_post" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<script type="text/javascript">
    function ValidateFileLimit(sender, args) {
        var filecount = document.getElementById('upload').files.length;
        if (filecount > 4) {
            alert('Please select only 4 images');
            return false;
        }
        else if (filecount <= 0) {
            alert('Please select atleast 1 image');
            return false;
        }
        return true;
    }
</script>

<div class="right_col" role="main">
<div class="">
<div class="page-title">
<div class="title_left">
<h3>Project Registration</h3>
</div>
</div>
<div class="clearfix"></div>

<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
    <div class="x_title">
    <h2>Registration Form <small></small></h2>
    
    <div class="clearfix"></div>
    </div>
    <div class="x_content">
    <br />
    <div class="row">

        <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
                  
            <div class="x_content">

               <div  data-parsley-validate class="form-horizontal form-label-left">
                   <asp:Label ID="Label3" runat="server" Visible="false"></asp:Label>
                      <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                        <asp:TextBox ID="maxcost" runat="server" class="form-control" placeholder="Maximum Price"></asp:TextBox>
                        <span class="fa fa-money form-control-feedback right" aria-hidden="true"></span>
                      </div>
                      <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback" style="color: #99B2D8">
                        <asp:FileUpload ID="upload" runat="server" CssClass="form-control" AllowMultiple="true" />
                        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator" ControlToValidate="upload" ClientValidationFunction="ValidateFileLimit"></asp:CustomValidator>
                        <asp:Label ID="photo" runat="server" Text=""></asp:Label>
                        <asp:Label ID="lblmsg" runat="server"></asp:Label> 
                         
                      </div>

                      <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                        <asp:TextBox ID="date" runat="server" class="form-control" TextMode="Date" ForeColor="#BBBBBB" placeholder="Maximum Price" ClientIDMode="Inherit"></asp:TextBox>
                        
                      </div>

                    </div>
                   
        
                   <div class="form-group">
        
        <div class="col-md-12 col-sm-12 col-xs-12 ">
        <div class="ln_solid"></div>
            <asp:Button ID="submit" runat="server" class="btn btn-primary" Text="Submit" OnClick="submit_click"  />
            <asp:Button ID="edit" runat="server" class="btn btn-primary" Text="Update" OnClick="update_click" />
        </div>
        </div>
        
            </div>
            <!-- End SmartWizard Content -->
                   
            </div>
                
        </div>
        </div>
    </div>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery -->
    <script src="../Builder/vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../Builder/vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="../Builder/vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="../Builder/vendors/nprogress/nprogress.js"></script>
    <!-- jQuery Smart Wizard -->
    <script src="../Builder/vendors/jQuery-Smart-Wizard/js/jquery.smartWizard.js"></script>
    <!-- Custom Theme Scripts -->
    <script src="../Builder/build/js/custom.min.js"></script>
</asp:Content>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download