Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:03:40+00:00 2026-06-05T19:03:40+00:00

I’ve got a simple js/jquery function I want to run when the form is

  • 0

I’ve got a simple js/jquery function I want to run when the form is submitted to do extra validation. My form allows multiple file inputs, and I want to make sure the combined total of all files is under a file limit I’ve set.

var totalFileSize = 0;
$("input:file").each(function () {
    var file = $(this)[0].files[0];
    if (file) totalFileSize += file.size;
});

return totalFileSize < maxFileSize; // I've set maxFileSize elsewhere.

The problem is that I want to run this as part of jquery validation. My form, in other places, uses standard MVC3 validation and a separate custom unobtrusive validation I’ve wrote. If this File Size validator fails, I want it to behave like those other jquery validators do: I want to, obviously, stop the submit, and display an error message in the same summary box as the others.

Is there some way to just call a simple method like this as part of validation on submit? I thought about $.validator.addMethod, but if I were to add that to each input:file element it would run the same validator multiple times on submit, and thus show the error message more than once. It would be great if there’s a way to add this validator but not tie it to any elements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:03:41+00:00Added an answer on June 5, 2026 at 7:03 pm

    You could write a custom validation attribute and register a custom client side adapter. Let me elaborate.

    Let’s suppose that you have a view model to represent a list of files to upload and that you want to limit the total size of all uploaded files to 2 MB. Your view model might certainly look something along the lines of:

    public class MyViewModel
    {
        [MaxFileSize(2 * 1024 * 1024, ErrorMessage = "The total file size should not exceed {0} bytes")]
        public IEnumerable<HttpPostedFileBase> Files { get; set; }
    }
    

    Now let’s define this custom [MaxFileSize] validation attribute which will obviously perform the server side validation but in addition to that it will implement the IClientValidatable interface allowing to register a custom unobtrusive client side validation rule that will allow to transpose this validation logic on the client (obviously only for browsers that support the HTML5 File API allowing you to determine on the client the size of the selected file => IE is totally out of the question for something like this and users that use this browser will have to satisfy them with server side only validation or do something better – use Internet Explorer for the only useful task in this world that this peace of software can do: go over the internet once you get a clean install of Windows to download a real web browser):

    public class MaxFileSizeAttribute : ValidationAttribute, IClientValidatable
    {
        public MaxFileSizeAttribute(int maxTotalSize)
        {
            MaxTotalSize = maxTotalSize;
        }
    
        public int MaxTotalSize { get; private set; }
    
        public override bool IsValid(object value)
        {
            var files = value as IEnumerable<HttpPostedFileBase>;
            if (files != null)
            {
                var totalSize = files.Where(x => x != null).Sum(x => x.ContentLength);
                return totalSize < MaxTotalSize;
            }
    
            return true;
        }
    
        public override string FormatErrorMessage(string name)
        {
            return base.FormatErrorMessage(MaxTotalSize.ToString());
        }
    
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = FormatErrorMessage(MaxTotalSize.ToString()),
                ValidationType = "maxsize"
            };
            rule.ValidationParameters["maxsize"] = MaxTotalSize;
            yield return rule;
        }
    }
    

    The next step is to have a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            if (!ModelState.IsValid)
            {
                // Server side validation failed => redisplay the view so
                // that the user can fix his errors
                return View(model);
            }
    
            // Server side validation passed => here we can process the
            // model.Files collection and do something useful with the 
            // uploaded files knowing that their total size will be smaller
            // than what we have defined in the custom MaxFileSize attribute
            // used on the view model
            // ...
    
            return Content("Thanks for uploading all those files");
        }
    }
    

    and a corresponding view:

    @model MyViewModel
    
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    <script type="text/javascript">
        (function ($) {
            $.validator.unobtrusive.adapters.add('maxsize', ['maxsize'], function (options) {
                options.rules['maxsize'] = options.params;
                if (options.message) {
                    options.messages['maxsize'] = options.message;
                }
            });
    
            $.validator.addMethod('maxsize', function (value, element, params) {
                var maxSize = params.maxsize;
                var $element = $(element);
                var files = $element.closest('form').find(':file[name=' + $element.attr('name') + ']');
                var totalFileSize = 0;
                files.each(function () {
                    var file = $(this)[0].files[0];
                    if (file && file.size) {
                        totalFileSize += file.size;
                    }
                });
                return totalFileSize < maxSize;
            }, '');
        })(jQuery);
    </script>
    
    
    @Html.ValidationMessageFor(x => x.Files)
    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div>
            @foreach (var item in Enumerable.Range(1, 3))
            {
                @Html.TextBoxFor(x => x.Files, new { type = "file" })
            }
        </div>
        <button type="submit">OK</button>
    }
    

    Obviously the javascript shown here have nothing to do inside a view. It must go into a separate reusable javascript file that the view could reference. I have included it inline here for more readability and ease of reproducing the scenario but in a real world never write inline javascript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.