R – ASP.Net FileUpload Control with Regex Validator postback problem


I'm trying to use a .Net FileUpload control along with a Regex Validator to limit filename to JPG, GIF, or PNG extensions. After postback, the filename is gone from the control (as expected), but this seems to cause the validator to fire and display its error text.

Can anyone suggest a fix, or a better way? Thank you!

Best Solution

Just use a custom validator with the following javascript function:

function UploadFileCheck(source, arguments)
    var sFile = arguments.Value;
    arguments.IsValid = 
       ((sFile.endsWith('.jpg')) ||
        (sFile.endsWith('.jpeg')) ||
        (sFile.endsWith('.gif')) ||

The custom validator code:

<asp:CustomValidator ID="cvalAttachment" runat="server" ControlToValidate="upAttachment" SetFocusOnError="true" Text="*" ErrorMessage="Invalid: File Type (allowed types: jpg, jpeg, gif, png)" ClientValidationFunction="UploadFileCheck"></asp:CustomValidator>

That should be all your need to stop it client side before it gets posted back.

Related Question