Question

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.

Was it helpful?

Solution

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 +")$";
    }
}

OTHER TIPS

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

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!

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>

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top