Visual Studio .NET - URL Validation - Asked By Byron King on 19-Jul-07 01:47 PM

Hi.  I need to store my validation expression strings in the web.config file.  For URL validation, I have the following code in my aspx file:

<asp:TextBox ID="txtRedirectUrl" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtRedirectUrl" Display="dynamic"
        Text="Please enter a valid URL" ValidationExpression='<%# WebConfigurationManager.AppSettings["UrlValExpression"] %>' runat="server" />

My web.config entry looks like this:


<appSettings>
   <add key="UrlValExpression" value=" (http(s)?://)([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?|([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)? " />      
</appSettings>

For whatever reason, it's not working.  Nothing passes validation; not even valid URL's.  Then the app is stuck on the page; i.e., I cannot backspace out the text and try again.  I have to do a postback in order to attempt the validation again.

I need to be able to retrieve the validation expression from the aspx page, and not the code behind file.  Thanks for the help.

Instead of using those funky inline directives, - Peter Bromberg replied to Byron King on 19-Jul-07 03:26 PM

consider something like this:

Page_Load (....

RegularExpressionValidator1.ValidationExpression = WebConfigurationManager.AppSettings["UrlValExpression"] ;


That's it! - Byron King replied to Peter Bromberg on 19-Jul-07 03:48 PM

Awesome, thanks!