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 8875397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:57:57+00:00 2026-06-14T18:57:57+00:00

I have a requirement to do multiple validations on a file upload control. I

  • 0

I have a requirement to do multiple validations on a file upload control. I have the following code for now:

<asp:Button id="btnUploadFile" class="ms-ButtonHeightWidth" runat="server"   OnClick="UploadFile_Click" Text="Upload File"></asp:Button>
<asp:RequiredFieldValidator ID="InputFileValidator" ControlToValidate="InputFile" Text="You must specify a value for the required field" runat="server" />

I need to add this ^(?!..)(?!...)(?=.[^.]$)[^\”#%&:<>?\/{|}~]{1,128}$ Regex validation from here in addition to the required field validator. How do I do this?

  • 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-14T18:57:58+00:00Added an answer on June 14, 2026 at 6:57 pm

    UPDATE:

    You could probably adapt the regex instead to allow for backslashes up to the filename and disallow them in the filename, but the complexity of such a beast would not likely be worth the time and effort to construct it.

    Since the original regex was for validating a textbox where the user was typing a filename (and not a file input where the name is generated by the OS), I think the better course of action would be to use an <asp:CustomValidator> control instead and split the value on \ to get more easily parseable chunks.

    The primary advantage of this approach is that you can break that complex regex down into multiple simpler (and more easily understood) regexes and test them one at a time against the filename.

        <script type="text/javascript">
            var validateFile = function validateFile(sender, args) {
                'use strict';
                var fileWithPath, //split on backslash
                    fileName = fileWithPath[fileWithPath.length - 1], //grab the last element
                    containsInvalidChars = /["#%&*:<>?\/{|}~]/g, //no reason to include \ as we split on that.
                    containsSequentialDots = /[.][.]+/g, //literal .. or ... or .... (etc.)
                    endsWithDot = /[.]$/g, // . at end of string
                    startsWithDot = /^[.]/g, // . at start of string
                    notValid = false, //boolean for flagging not valid
                    valid = fileName.length > 0 && fileName.length <= 128;
                notValid = containsInvalidChars.test(fileName);
                notValid = notValid || containsSequentialDots.test(fileName);
                notValid = notValid || endsWithDot.test(fileName);
                notValid = notValid || startsWithDot.test(fileName);
                args.IsValid = valid && !notValid;
            };
        </script>
        <asp:FileUpload ID="InputFile" runat="server" />
        <asp:RequiredFieldValidator ID="rqfvInputFile" runat="server" ControlToValidate="InputFile" ErrorMessage="File is required"></asp:RequiredFieldValidator>
        <asp:CustomValidator ID="cstvInputFile" runat="server" ControlToValidate="InputFile" ClientValidationFunction="validateFile" ErrorMessage="File is not a sharepoint file"></asp:CustomValidator>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    

    One caveat to the above is that the filename chunks are split on \, which is not likely to be the path separator for Unix or Mac systems. If you need this to run on those clients as well, you’ll likely have to split on either \ or / which you should be able to do with this:

    var filePath = args.Value.split(/\\|\//g); //not tested.
    

    ORIGINAL:

    Add in an <asp:RegularExpressionValidator> control and set the ControlToValidate property to your file uploader control.

    You can have as many validator controls as you like pointed towards a single input.

    Just set the appropriate properties (such as the ValidationExpression in the <asp:RegularExpressionValidator>) and make sure the ControlToValidate property is pointed towards the input to validate.

    Example:

    <asp:Button id="btnUploadFile" class="ms-ButtonHeightWidth" runat="server" OnClick="UploadFile_Click" Text="Upload File"></asp:Button>
    <asp:RequiredFieldValidator runat="server" ID="RequiredInputFileValidator" ControlToValidate="InputFile" Text="You must specify a value for the required field" />
    <asp:RegularExpressionValidator runat="server" ID="RegexInputFileValidator" ControlToValidate="InputFile" ErrorMessage="Only valid SharePoint files are allowed."
        ValidationExpression="^(?!..)(?!...)(?=.[^.]$)[^\"#%&:<>?\/{|}~]{1,128}$" />
    

    You may also want to look into Validation groups

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

Sidebar

Related Questions

I have got the requirement to find the core file in multiple box/machine and
I have a requirement for a set of asp.net MVC websites as follows: Multiple
I have requirement to create an asp.net wizard control which will be re usable
I have a requirement to download multiple CSV files from a remote FTP site.
I have a requirement to store the list of services for multiple computers. I
I have a requirement that in a webpage I have multiple block of content
I have requirement as following like showing ABPeoplePickerNavigationController in tabbar based application. I researched
I have a requirement to assign additional information to multiple contacts in Audience Manager,
We have a requirement to delete rows in the order of millions from multiple
I have a requirement to have multiple iframes on one page, that all interact

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.