articles

Home / DeveloperSection / Articles / Validating FileUpload control for specific file type and required field.

Validating FileUpload control for specific file type and required field.

Validating FileUpload control for specific file type and required field.

Anonymous User 80970 01-Jan-2011

Validating FileUpload control for specific file types and required fields.

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

For validating for the 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 a brief idea about the controls required.

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

FileUpload Control: 

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

For detailed reading about the FileUpload visit:

https://www.mindstick.com/Articles/176/fileupload-control-in-asp-dot-net

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

RequiredFieldValidator: 

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:  https://www.mindstick.com/Articles/195/requiredfieldvalidator-control-in-asp-dot-net


RegularExpressionValidator: 

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:

https://www.mindstick.com/Articles/197/regularexpressionvalidator-control-in-asp-dot-net

Validating FileUpload Control in ASP.Net

Validating File Upload Control 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 are:

"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.txt)$"

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:

"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.pdf)$"

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

<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.



Updated 25-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By