tags:

views:

9643

answers:

6

I have a .NET webform that has a file upload control that is tied to a regular expression validator. This validator needs to validate that only certain filetypes should be allowed for upload (jpg,gif,doc,pdf)

The current regular expression that does this is:


^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF|.doc|.DOC|.pdf|.PDF)$

However this does not seem to be working...Can anyone give me a little reg ex help?

Thanks.

+16  A: 

Your regex seems a bit too complex in my opinion. Also, remember that the dot is a special character meaning "any character". The following regex should work (note the escaped dots):

^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF)$

You can use a tool like Expresso to test your regular expressions.

Dario Solera
When doing regular expressions in .NET, enumerating casing differences is not required. It not only can decrease readability, but also can degrade performance if it is called in a loop, for example.
joseph.ferris
The problem is that the regex is used in a RegularExpressionValidator ASP.NET control, which AFAIK does not accept options such as IgnoreCase.
Dario Solera
I missed that in the original post. Yes, RegularExpressionValidator case-sensitivity options are something that Microsoft has been ignoring the pleas from the community at large for a few years now.
joseph.ferris
You can leave out the ^.* as "match anything from the beginning up until this expression at the end" is the same as "match this expression at the end". You can also embed regular expression options http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx
ICR
In order to embed the regular expression option to ignore case you need to disable ClientSide script (I don't think JavaScript support it). You can then use use "(?i:.(jpg|gif|doc|pdf))$" for a case insensitive match.
Martin Brown
What happens if you have an extension that is mixed case? Like .Doc ?
Dan Diplo
+3  A: 

Are you just looking to verify that the file is of a given extension? You can simplify what you are trying to do with something like this:

(.*?)\.(jpg|gif|doc|pdf)$

Then, when you call IsMatch() make sure to pass RegexOptions.IgnoreCase as your second parameter. There is no reason to have to list out the variations for casing.

Edit: As Dario mentions, this is not going to work for the RegularExpressionValidator, as it does not support casing options.

joseph.ferris
+2  A: 

Your regexp seems to validate both the file name and the extension. Is that what you need? I'll assume it's just the extension and would use a regexp like this:

\.(jpg|gif|doc|pdf)$

And set the matching to be case insensitive.

PEZ
+4  A: 

You can embed case insensitity into the regular expression like so:

\.(?i:)(?:jpg|gif|doc|pdf)$
ICR
Except that this fails if you leave the client script option enabled.
Martin Brown
Afaik javascript does allow inline options, but it applies to the whole regex and not just everything after it, which doesn't matter in this case. Unless there is another reason it won't work (I can't test atm).
ICR
No, JS doesn't support inline modifiers at all. Also, your regex won't work even in .NET; you want either "\.(?i)(?:jpg|gif|doc|pdf)$" or "\.(?i:jpg|gif|doc|pdf)$". This: "(?i:)" just matches nothing, case-insensitively.
Alan Moore
A: 

(.*?).(jpg|gif|doc|pdf)$ this expression works fine and show an error message works fine in IE but doesn't effect on FireFox why tell me

A: 

Because vbscript supports only freakin IE

marko