Solved

Regular Expression for only allowing certain file type

Posted on 2009-05-20
4
231 Views
Last Modified: 2012-05-07
Hi All,

I have the following feable attempt to restrict my .net asp:FileUpload control to only allow JPG, GIF, and PNG files to be uploaded but it seems to not allow any files to be uploaded.

Please could someone suggest what I am doing wrong or an alternative expression?

Many thanks,

Rit
<asp:RegularExpressionValidator 
 id="RegularExpressionValidator1" runat="server" 
 ErrorMessage="Only jpg, png or gif files are allowed!" 
 ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
    +(.jpg|.png|.gif)$" 
 ControlToValidate="FileUploadLogo"></asp:RegularExpressionValidator>

Open in new window

0
Comment
Question by:rito1
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24430608
do your files match the ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
 +part?
0
 
LVL 1

Author Comment

by:rito1
ID: 24430695
Ozo, I will be totally honest with you.. I haven't a clue what that statement means!

Basically I only want to allow .jpg, .gif and png files to be uploaded.

Rit
0
 
LVL 16

Accepted Solution

by:
jessc7 earned 500 total points
ID: 24431382
I think you could simplify it considerably, if you are only concerned with the extension:

^(.)+(.jpg|.png|.gif)$

Does tha work for you?
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 31583458
Thanks jessc7, that worked perfect.

Rit
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question