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?
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.
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:ORIGINAL:
Add in an
<asp:RegularExpressionValidator>control and set theControlToValidateproperty 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
ValidationExpressionin the<asp:RegularExpressionValidator>) and make sure theControlToValidateproperty is pointed towards the input to validate.Example:
You may also want to look into Validation groups