Regular Expression to validate file extension

C

Chris Kennedy

Does anyone know a regular expression that will validate the file extension
but also allow multiple file extensions if necessary. It also needs to be
case insensitive. Basically, what I want is to validate a file input box to
check if the extension is the correct type, i.e. .doc for a Word Document
etc. Also I would like to check multiple file types, for instance allow a
gif or a jpeg or a jpg.

Regards, Chris.
 
K

Kevin Spencer

I responded to this exact same question yesterday.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
B

bloomfield

Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} NOT OK", test));
}
}

Hope this helps
 
C

Chris Kennedy

Sorry about repost but my news server sometimes gets confused and appends
the question to the wrong thread. I couldn't reply because I couldn't find
it. You can sometimes find it on Google but not always. Has question not
clarified things. I want an expression to check for a variable number of
expressions in the extension of a file path e.g. jpeg or jpg or gif. I hope
this clarifies things. Regards, Chris.
 
K

Kevin Spencer

In that case, Chris, it looks like boomfield has already guessed your
question, and given you a good answer!

BTW, there is an excellent freeware utility for building and testing Regular
Expressions, which you can download from:

http://www.weitz.de/regex-coach/

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

Chris Kennedy

It tried putting this in the validation expression of my regular expression
validator, got it to validate a textbox with qwerty.jpg as the text value
and it didn't work. Any ideas? Regards, Chris.

bloomfield said:
Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} NOT OK", test));
}
}

Hope this helps

--
____________________
www.bloomfield.as.ro

Chris said:
Does anyone know a regular expression that will validate the file extension
but also allow multiple file extensions if necessary. It also needs to be
case insensitive. Basically, what I want is to validate a file input box to
check if the extension is the correct type, i.e. .doc for a Word Document
etc. Also I would like to check multiple file types, for instance allow a
gif or a jpeg or a jpg.

Regards, Chris.
 
C

Chris Kennedy

Sorry I'll be more specific, by didn't work I mean it it saw valid (ie
qwerty.jpg) and invalid strings (eg qwerty.doc) as invalid.


Chris Kennedy said:
It tried putting this in the validation expression of my regular expression
validator, got it to validate a textbox with qwerty.jpg as the text value
and it didn't work. Any ideas? Regards, Chris.

bloomfield said:
Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} NOT OK", test));
}
}

Hope this helps

--
____________________
www.bloomfield.as.ro

Chris said:
Does anyone know a regular expression that will validate the file extension
but also allow multiple file extensions if necessary. It also needs to be
case insensitive. Basically, what I want is to validate a file input
box
allow
 
B

bloomfield

I tried the expression ^.+\.((jpg)|(gif)|(jpeg))$ and it worked for me
in a RegularExpressionValidator. The only problem was with the string
case. I didn't find a way to specify that the regular expression is case
insensitive for the RegularExpressionValidator. The expression can be
rewritten to
^.+\.(([jJ][pP][eE]?[gG])|([gG][iI][fF]))$
to allow extensions like jPeG etc

Chris said:
Sorry I'll be more specific, by didn't work I mean it it saw valid (ie
qwerty.jpg) and invalid strings (eg qwerty.doc) as invalid.


It tried putting this in the validation expression of my regular
expression

validator, got it to validate a textbox with qwerty.jpg as the text value
and it didn't work. Any ideas? Regards, Chris.

Here there are some clues

string checkFileRegex = @"^.+\.((jpg)|(gif)|(jpeg))$";

string[] testData = new string[]
{
@"D:\My\download\test1.gif",
@"D:\My\download\test2.cmd",
"TeSt3.jPg",
"test4.opp",
"test5.JPG",
"test6.gIn",
"test7.again.jpeg",
"nothing"
};

foreach(string test in testData)
{
if( Regex.IsMatch(test, checkFileRegex, RegexOptions.Multiline |
RegexOptions.IgnoreCase ))
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} OK", test));
}
else
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} NOT OK", test));
}
}

Hope this helps

--
____________________
www.bloomfield.as.ro

Chris Kennedy wrote:

Does anyone know a regular expression that will validate the file
extension

but also allow multiple file extensions if necessary. It also needs to
be

case insensitive. Basically, what I want is to validate a file input
box
to

check if the extension is the correct type, i.e. .doc for a Word
Document

etc. Also I would like to check multiple file types, for instance
allow
a

gif or a jpeg or a jpg.

Regards, Chris.
 
Joined
Sep 15, 2010
Messages
1
Reaction score
0
Here's a post that does file extension validation with jQuery. Am sure it could be easily changed to work with multiple files.

http: [forward slash foward slash] you.arenot.me/2010/09/15/file-uploader-with-javascript-extension-validation/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top