Sunday, May 31, 2009

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)$





0 comments: