0

Works in IE and Chrome. Can't find any help on Google. Basically, it's just checking the extension of the file selected in a FileUpload control.

Here's the code:

        <asp:FileUpload ID="FileUpload1" runat="server" Width="450" />
       <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid file type."
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.pdf|.txt|.doc|.csv|.xls|.xlsx)$" 
ControlToValidate="FileUpload1" Display="Dynamic">
</asp:RegularExpressionValidator>

2 Answers 2

4

There are other characters in the file path in file up loader therefore, try this Regular expression instead

(.*\.(pdf|txt|doc|csv|xls|xlsx)$)

And your RegularExpressionValidator looks like

<asp:RegularExpressionValidator id="revImage" runat="server"
   ErrorMessage="Please Upload the Valid document File" Text="*" Display="Dynamic"
   ValidationExpression=
      "(.*\.(pdf|txt|doc|csv|xls|xlsx)$)"
   ControlToValidate="fileUploaderId"> </asp:RegularExpressionValidator>
Sign up to request clarification or add additional context in comments.

Comments

1

The regular expression is too restrictive. I'm not sure why it works in IE & Chrome but not in Firefox. Perhaps Firefox is stripping the path of the filename?

Your reg ex is trying to match a full Windows filepath, so would fail if it the name didn't look like that, say if you used a Mac, or Linux machine or the browser pre-stripped the path from the name.

if you change it to this:

`.(pdf|txt|doc|csv|xls|xlsx)$' it only ensures the names ends with an approved extension (this just a slight improvement on what @waqas posted)

Simon

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.