Click here to Skip to main content
12,764,666 members (40,424 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 25 Nov 2011

Asp.Net Custom Validator Control to validate file size

, 25 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Asp.Net Custom Validator Control to validate file size both client side validation and server side validation
The following code is used to validate the file size before uploading the file.
We use the custom validation control to achieve this. We can use
custom validation control to validate either client side or at server side. We see
both the things for this purpose.

Asp.Net Custom Validation Control Client Side Validation to Validate File Size:
 <script type ="text/javascript">

    function checkfilesize(source, arguments) {
        arguments.IsValid = false; 

        var axo = new ActiveXObject("Scripting.FileSystemObject");
        thefile = axo.getFile(arguments.Value);
        var size = thefile.size;
        if (size > 5000) {
            arguments.IsValid = false;
        else {
            arguments.IsValid = true;


Asp.Net Custom Validation Control Server Side Validation to Validate File Size:
  <asp:FileUpload ID="FileUpload1" runat="server" /><br />
    <asp:Button ID="Button1" runat="server" Text="Upload"/>
    <br />
    <asp:CustomValidator ID="NewPasswordCustomValidator" runat="server"  
                    Text="*" ToolTip="FileSize should not exceed 5kb" 
                    ErrorMessage="FileSize Exceeds the Limits.Please Try uploading smaller size files." 
                    OnServerValidate = "checkfilesize">
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />

The C# function to validate:
protected void checkfilesize(object source, ServerValidateEventArgs args)
        string data = args.Value;
        args.IsValid = false;
        double filesize = FileUpload1.FileContent.Length;
        if (filesize > 5000)
            args.IsValid = false;
            args.IsValid = true;

I hope this simple technique would help you to perform your custom validations

Understanding The Code:
The code describe is very simple and helps you achieve the task. Initially start
by adding the custom server control and then define the following properties to it:
1. Text: The text to be displayed.
2. Error Message: Error message for the user.
3. ControlToValidate: Validation control on which the validation should be performed.
and finally we have two things

4.OnServerValidate : the name of function if you want to validate by using your server side langauge.
ClientValidationFunction : the name of JavaScript function if you want to validate the client side.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

S V Saichandra
Software Developer Emirates Group IT
United Arab Emirates United Arab Emirates
S V Sai Chandra is a Software Engineer from Hyderabad Deccan. He started Embedded Programing in his college days and now he is a Web Developer by Profession. He Loves coding and his passion has always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books, painting and hang out with friends is his most favorite past time hobby.
He blogs at
Technical Skills:
C#,Ado.Net,Asp.Net,Sql Server,JavaScript,XML,Web services.

You may also be interested in...


Comments and Discussions

QuestionNot working why? its so Pin
Ajay kumar sahu23-Nov-14 21:45
memberAjay kumar sahu23-Nov-14 21:45 
AnswerRe: Not working why? its so Pin
Alejandro Del Genio8-Sep-15 8:02
memberAlejandro Del Genio8-Sep-15 8:02 
Questionwhat if i want the validation to occur before i press the upload button Pin
pree_kh29-Nov-12 23:34
memberpree_kh29-Nov-12 23:34 
GeneralYour client-side validation code will only work in Internet ... Pin
Richard Deeming1-Dec-11 5:44
memberRichard Deeming1-Dec-11 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 25 Nov 2011
Article Copyright 2011 by S V Saichandra
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid