Search code examples
asp.netregexfile-uploadcase-insensitivefile-type

Problem validating asp:UploadFile file names with RegularExpressionValidator


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.


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