views:

569

answers:

5

I have in the settings file a row of all the file types I want to allow:

jpeg|jpg|tiff|tif|png|gif|bmp|eps|wmf|emf|pdf|doc|docx|zip|rar|ppt|pptx|mdb|xls

I want to have next to the FileUpload control a RegularExpressionValidator that allows only these files.

I am handling it in the PageLoad event setting the ValidationExpression property of the regex validator.

i tried:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string regex = "jpeg|jpg|tiff"; //A huge list of filetypes.
        upFiles_RegularExpressionValidator.ValidationExpression = 
            @"^.*\.(" + regex +")$";
    }
}

But it's case sensitive. My only problem now is to make it insensitive.

A: 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string regex = "([jJ][pP][eE][gG])|([jJ][pP][gG])|([tT][iI][fF][fF])"; //A huge list of filetypes.
        upFiles_RegularExpressionValidator.ValidationExpression = 
            @"^.*\.(" + regex +")$";
    }
}
Keith
A: 

If you turn off client side validion and only use server side validation, there is a .NET support case insensitivte operatior (?i) that can be used. If you want somethign that works both client and server side you may need to resort to something like

[Jj][Pp][Ee][Gg]

for each of your file extensions. Didn't include code for each of your extensions, should be pretty easy to extrapolate the pattern

James Conigliaro
Can yuo please explain the differences between these two options?
Shimmy
And how to use them
Shimmy
The case insensitive operator - (i?) is a .NET specific operator and not part of the standard regular expression syntax. Client side validators use JavaScipt, so this operator does not work on the client side. If you are okay with only using server side validation, then disable the client side validation and use the operator.The second option is equivelant of saying, for each extension Capitor or lower case j followed by capitol or lower case p followed by capitol or lower case e followed by capitol or lower case g the brackets [] define a character set.
James Conigliaro
Isn't there any JS case in. operator?I am not planning to use [Jj] etc.
Shimmy
yes - /i - but because the syntax is different than the .NET equivelant you need to pick either client side or server side validation for the validator control. If you want both, then you would probably need to use a custom validator.
James Conigliaro
A: 

Answer:

^.*\.(?i-s:pdf|exe)$

Which means:

ValidationExpression = @"^.*\.(?i-s:pdf|exe)$"; //will match pdf PDF exe EXE

string regex = "jpeg|jpg|tiff|tif|png|gif|bmp|eps";
ValidationExpression = @"^.*\.(?i-s:file_types)$".Replace("file_types", regex);

This should be a very efficient way to validating files against a dynamic changeable list

FYI, I made it with this online regex builder, an amazing free tool!

Shimmy
Does that work on the client side as well?
Sean Bright
ouch!i just tested it our and it doesn't!
Shimmy
Ouch!! It's valid regex, but it doesn't work in the asp:RegularExpressionValidator!Is there any way to make the regex of this control case insensitive?
Shimmy
A: 

The regex works fine with the RegularExpressionValidator on the server-side. The problem rises when it tries to do the client validation, which fails because the javascript regex flavor doesn't know how to handle "?i" (case insensitivity is achieved in javascript regex with "i"). You can solve this problem by adding this script to your pages. I think the script is pretty straight forward.

   <script type="text/javascript" language="javascript"> 
        function RegularExpressionValidatorEvaluateIsValid(val) 
        { 
          var value = ValidatorGetValue(val.controltovalidate); 
          if (ValidatorTrim(value).length == 0) 
            return true; 

          var regex = null; 
          if(val.validationexpression.indexOf("(?i)")>=0) 
          { 
             regex = new RegExp(val.validationexpression.replace("(?i)",""),"i"); 
          } 
          else 
          { 
             regex = new RegExp(val.validationexpression); 
          } 

          var matches = regex.exec(value);

          return (matches != null && value == matches[0]); 
        } 
   </script>
Dumb Questioner
A: 

If you are doing this validation server-side with .NET, you can use something like

VB:

    Dim re As New Regex("(jpg)|(gif)", RegexOptions.IgnoreCase)
    re.IsMatch("image.jpg")

C#:

    var re = new Regex("(jpg)|(gif)", RegexOptions.IgnoreCase);
    re.IsMatch("image.jpg")
Jeff
I was looking for a way to validate with the ASP.NET RegularExpressionValidator control.
Shimmy