Home > Asp.net > Validating Extension(eg. (.aspx/.html/.jpeg….) in asp.net using Regular Expression

Validating Extension(eg. (.aspx/.html/.jpeg….) in asp.net using Regular Expression


When you want to validate that the text entered must have particular extension that you have decided.

for example if you have a textbox to enter page name. You want the user to enter page name with “.aspx” extension. Then you have to use the following regular expression validator.
<asp:RegularExpressionValidator ErrorMessage=” Please Enter page name with .aspx extension”
ID=”RegularExpressionValidator1″ runat=”server”
ValidationExpression=”^.*\.(aspx)$” ControlToValidate=”txturl” Display=”Dynamic” ></asp:
RegularExpressionValidator >

so now if the user enters “hello.abc” then validation will fire with message “Please enter page name with .aspx extension”

Thus with this ValidationExpression=”^.*\.(aspx)$”

you can specify any extension like gif, jpeg or whatever you want..

^.*\.(exe|bat|etc)$



Advertisements
  1. Sumant
    June 9, 2014 at 9:50 am

    Thanks

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: