Wednesday, June 10, 2009

Use RegularExpressionValidator to validate a URL / web address

This is a nice RegularExpressionValidator to validate URLs:

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="uxRssUrl" ErrorMessage="<%$ Resources:Texts, InvalidUrl %>"
ValidationExpression="^(ht|f)tp(s?)\:\/\/([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z]))*\.([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z]))*\.(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$"
ValidationGroup="ResellerValidation"></asp:RegularExpressionValidator>">

This RegularExpressionValidator will check whether the content starts with:
(ftp:// ftps:// http:// https://) and before the next '/' comes whether there is at least 2 '.' in it.

This site is helpful for RegularExpressionValidator:
How To: Use Regular Expressions to Constrain Input in ASP.NET

No comments: