Click here to Skip to main content
12,825,861 members (34,519 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0 jQuery

I have a problem in uploading the file name for an image. When ever I try to Upload an image with the name having special characters, during the preview it creates problem. So I want to restrict the file upload control to restrict the entry of the special characters.

Here is an example of the image name that is creating the problem.

Here the 2nd character is creating problem for me. So I want to apply validation for the entry of the special characters in the file upload control. Any help will be appreciated. Thanks in advance.
Posted 6-Jan-13 23:15pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

To upload images to the server from Asp.Net web application, we will use the FileUpload server control. This control can be validated to upload only Image files using the RegularExpressionValidator control.

To achieve this drag and drop a FileUpload and RegularExpressionValidator control into your aspx page. Then set the ControlToValidate property as “FileUpload1” and the ValidationExpression property as specified below.

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

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"
 ErrorMessage="Invalid Image File"
"^([0-9a-zA-Z_\-~ :\\])+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.gif|.GIF|.png|.PNG)$">

So the above RegularExpressionValidator will only allow jpg, jpeg, bmp, gif, png formats image files. You can add more file formats separated by “|” symbol. Also you can specify more special characters after the string “_\-~” to allow the files you want to upload to the server.

Good luck,
Orcun Iyigun 7-Jan-13 8:47am
If you keep the validation expression as above it will automatically block the
What is ambigious here? I think my answer is pretty understandable. If you do not want user adding a file which contains "å" that character keep the regular expression like above. But if you change that expression to "^([0-9a-zA-Z_\-~ :\\å])+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.gif|.GIF|.png|.PNG)$"> this. user can add a file which contains "å" that character in the path. Have you tried my codeand tested it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100