Regular Expression to validate file extension

Discussion in 'ASP .Net' started by Chris Kennedy, Jun 18, 2004.

  1. 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.
     
    Chris Kennedy, Jun 18, 2004
    #1
    1. Advertising

  2. 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.

    "Chris Kennedy" <> wrote in message
    news:#...
    > 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.
    >
    >
     
    Kevin Spencer, Jun 18, 2004
    #2
    1. Advertising

  3. Chris Kennedy

    bloomfield Guest

    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.
    >
    >
     
    bloomfield, Jun 18, 2004
    #3
  4. 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.


    "Kevin Spencer" <> wrote in message
    news:...
    > 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.
    >
    > "Chris Kennedy" <> wrote in message
    > news:#...
    > > 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.
    > >
    > >

    >
    >
     
    Chris Kennedy, Jun 18, 2004
    #4
  5. 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.

    "Chris Kennedy" <> wrote in message
    news:u$...
    > 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.
    >
    >
    > "Kevin Spencer" <> wrote in message
    > news:...
    > > 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.
    > >
    > > "Chris Kennedy" <> wrote in message
    > > news:#...
    > > > 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.
    > > >
    > > >

    > >
    > >

    >
    >
     
    Kevin Spencer, Jun 18, 2004
    #5
  6. 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" <> wrote in message
    news:#...
    > 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.
    > >
    > >
     
    Chris Kennedy, Jun 19, 2004
    #6
  7. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:#...
    > > 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.
    > > >
    > > >

    >
    >
     
    Chris Kennedy, Jun 19, 2004
    #7
  8. Chris Kennedy

    bloomfield Guest

    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 Kennedy wrote:

    > 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" <> wrote in message
    > news:...
    >
    >>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" <> wrote in message
    >>news:#...
    >>
    >>>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.
    >>>>
    >>>>

    >>
    >>

    >
    >


    --
    ____________________
    www.bloomfield.as.ro
     
    bloomfield, Jun 21, 2004
    #8
  9. Chris Kennedy

    colin.wiseman

    Joined:
    Sep 15, 2010
    Messages:
    1
    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/
     
    colin.wiseman, Sep 15, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,354
  2. Ori
    Replies:
    2
    Views:
    21,348
    Brian W
    Jan 26, 2004
  3. Nicolas LEHUEN
    Replies:
    4
    Views:
    524
    darrell
    Nov 27, 2003
  4. Nicolas Lehuen
    Replies:
    2
    Views:
    313
    Nicolas LEHUEN
    Nov 21, 2003
  5. Replies:
    3
    Views:
    159
    Dr J R Stockton
    Jun 12, 2007
Loading...

Share This Page