Validating FileUpload control for specific file type and required field.

In this article I am going to explain how we can check and validate user for uploading only certain files and making uploading of file to be must for submission of form.

For validating for required field we have to use RequiredFieldValidator and for validating for file types we’ll have to use RegularExpressionValidator. But, before starting we should have brief idea about the controls required.

Before starting about validating FileUpload we must have brief idea about FileUpload control and Validation Controls of ASP.Net

FileUpload Control:

FileUpload control is used for uploading file to the server from the client side.

For detailed reading about FileUpload visit:


With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is an empty string (“”).

For more details visit:


The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required.

For more details about RegularExpressionValidator visit following link:

Validating FileUpload Control in ASP.Net

Validating File Upload Control for certain file types. Here in the example below I am using RegularExpressionValidator to accomplish my task.

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Only .doc , .docx or .txt files are allowed." 
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.txt)$" ControlToValidate="FileUpload1">*</asp:RegularExpressionValidator>

Here, regular expression for .doc, .docx, .txt files is:


If you want some more or other files type to be uploaded then you can replace or add more extension with the above mentioned extensions. Suppose you want your user to upload only .pdf file then you Regular expression for that will be:


But, this does not mean that user will be forced to upload file, this validation is only performed if user tries to upload file. If you want to make uploading of file mandatory then you have to use RequiredFieldValidator.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="You have not uploaded your file" ControlToValidate="FileUpload1">*</asp:RequiredFieldValidator>

The code above will validate FileUpload1 for required field.

Set max length of MultiLine TextBox in ASP.Net

There was a requirement in a project where I had to set the max length of TextBox in Multiline mode so I just set the MaxLength property of TextBox to the length which was required but it doesn’t worked. So as usual I took help of search engines and found that MaxLength does not work in MultiLine mode and found two alternatives to fix the issue, one is through javascript and another through RegularExpressionValidator.

Here I am going to share the two ways which I found

  • Through Javascript:Here is the Javascript code which should be added.
    <script type="text/javascript">
         function Check(textBox, maxLength) {
              if (textBox.value.length > maxLength) {
                   alert("Max characters allowed are " + maxLength);
                   textBox.value = textBox.value.substr(0, maxLength);

    Now call the javascript function on two client side event of TextBox namely onKeyUp and onChange like this

    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
    onKeyUp="javascript:Check(this, 100);" onChange="javascript:Check(this, 100);" />
  • Through RegularExpressionValidator Use "^[\s\S]{0,100}$" as Validation Expression to validate through RegularExpressionValidator like this
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ControlToValidate="TextBox1" ErrorMessage="Exceeding 100 characters" ValidationExpression="^[\s\S]{0,10}$" />

    You can change the value to in braces according to the requirement. The first value in braces is minimum required length and second value is maximum required length of Multiline TextBox.

Here is all which I found. If you get any other solution then do add your solution in your comment and help others.

All suggestions are welcome.