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?
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.
You can use this template for every file type:
ValidationExpression="^.+\.(([pP][dD][fF])|([jJ][pP][gG])|([pP][nN][gG])))$"
for ex: you can add ([rR][aA][rR]
) for Rar file type and etc ...
You can embed case insensitity into the regular expression like so:
\.(?i:)(?:jpg|gif|doc|pdf)$
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.
^.+\.(?:(?:[dD][oO][cC][xX]?)|(?:[pP][dD][fF]))$
Will accept .doc, .docx, .pdf files having a filename of at least one character:
^ = beginning of string
.+ = at least one character (any character)
\. = dot ('.')
(?:pattern) = match the pattern without storing the match)
[dD] = any character in the set ('d' or 'D')
[xX]? = any character in the set or none
('x' may be missing so 'doc' or 'docx' are both accepted)
| = either the previous or the next pattern
$ = end of matched string
Warning! Without enclosing the whole chain of extensions in (?:), an extension like .docpdf would pass.
You can test regular expressions at http://www.regextester.com/
You can embed case insensitity into the regular expression like so:
\.(?i:)(?:jpg|gif|doc|pdf)$
You can use this template for every file type:
ValidationExpression="^.+\.(([pP][dD][fF])|([jJ][pP][gG])|([pP][nN][gG])))$"
for ex: you can add ([rR][aA][rR]
) for Rar file type and etc ...
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.
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.
You can embed case insensitity into the regular expression like so:
\.(?i:)(?:jpg|gif|doc|pdf)$
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.
You can embed case insensitity into the regular expression like so:
\.(?i:)(?:jpg|gif|doc|pdf)$
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.
Source: Stackoverflow.com