mahesh
mahesh

Reputation: 3217

how to validate a filename using asp.net regular expression validator

i have the following code to validate my file name entered using regular expression validator but even after enter correct file name format, its hitting error saying enter valid filename

<asp:TextBox ID="TxtFileName" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="FileNameRegularExpressionValidator" runat="server"
   ErrorMessage="Enter valid FileName"
   ControlToValidate="TxtFileName"
   ValidationExpression="^(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$">
</asp:RegularExpressionValidator>

Upvotes: 1

Views: 4436

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336178

At the moment, your regex requires the filename to start with a backslash. Also, your filenames may only contain the lowercase form of letters. Is that intentional?

Also, you're repeating your repeated group, a surefire recipe to bring your server down to its knees with catastrophic backtracking once someone enters an invalid filename that's more than a few characters long.

Perhaps

ValidationExpression="(?i)^[\w\s0-9.-]+\.(txt|gif|pdf|doc|docx|xls|xlsx)$">

would be more suitable?

Upvotes: 1

Related Questions