Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to do this:

<obout:OboutTextBox ID="txtDistributorEmail" runat="server" Width="250" />
    <asp:RegularExpressionValidator ID="revDistributorEmail" runat="server"     
    ErrorMessage="An invalid email address was entered."                                 
    ValidationExpression='<%=Mynamespace.RegEx.EMAIL %>' 
    ControlToValidate="txtDistributorEmail" />

namespace Mynamespace
{
    public class RegEx
    {
        public const string EMAIL = @"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z] 
        [-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
    }
 }

The validation is failing even when a correct email is entered. But if I replace the ValidationExpression with the regular expression [], it works fine.

Is there anything in-correct I am doing?

share|improve this question
 
Have a look at how the ValidationExpression is getting rendered in your HTML. That should tell you a lot. –  Ann L. Sep 14 '12 at 20:23

1 Answer

This goes on top of aspx page

<%@ Import Namespace="Mynamespace" %>  



<asp:RegularExpressionValidator ID="revDistributorEmail" runat="server"     
    ErrorMessage="An invalid email address was entered."                                 
    ValidationExpression="<%# Mynamespace.RegEx.EMAIL %>" 
    Display="Dynamic"
    ControlToValidate="txtDistributorEmail" />
share|improve this answer
 
Ann, i see this in HTML that's rendered: revDistributorEmail.evaluationfunction = "RegularExpressionValidatorEvaluateIsValid"; revDistributorEmail.validationexpression = "<%=RegEx.EMAIL %>"; –  Kiran D Sep 14 '12 at 20:47
 
oh boy - right click on the .aspx page with that code , then click view source , then the page load part goes in there , not in the HTML - or are you using MVC? –  Scott Selby Sep 14 '12 at 20:50
 
that's what I did. right-click and view source and that's the rendered HTML on the web page. –  Kiran D Sep 14 '12 at 20:53
 
Also ur comment earlier doesn't make any sense, the whole idea was not to hard-code the regular expression in the code-behind, but to store them in a class that I can re-use. –  Kiran D Sep 14 '12 at 20:54
 
oohhhhh , I got it , hold on , I thought you wanted to be able to dynamically change - not reuse , I got it now , hold on –  Scott Selby Sep 14 '12 at 20:55
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.