Question

I'm trying to put together a form and work within the ASP validation framework if possible. Recently it seemed prudent to re-think some of the validation and so I got rid of four validators and replaced them with one CustomValidator. Now I have two CustomValidators (previously I had one CustomValidator and four RegulorExpressionValidators).

So here's the weird thing. One of my custom validators was somehow showing the error messages from the validators I deleted. I've actually double checked and confirmed that the string it is displaying is no longer in the project. Nor is it anywhere in the directory I'm deploying to. (I've built, cleaned, and rebuilt).

Further, this did not appear to be a browser cache issue as I have the cache turned off (FF Web Developer). The problem would also change based on where I placed the validator in the page (again verifying that this was not some bizarro cache issue).

I was finally able to resolve it by content inside of the validator tag.

So this works:

<asp:CustomValidator
        ID="validateUrl"
        OnServerValidate="ValidateWebsiteUrl"
        ClientValidationFunction="isUrlValid" 
        ControlToValidate="websiteURLInput"
        ValidationGroup="CommentGroup"
        runat="server" 
        ForeColor="#991112"
>Please enter a valid URL</asp:CustomValidator>

But this fails:

<asp:CustomValidator
        ID="validateUrl"
        OnServerValidate="ValidateWebsiteUrl"
        ClientValidationFunction="isUrlValid" 
        ControlToValidate="websiteURLInput"
        ValidationGroup="CommentGroup"
        runat="server" 
        ForeColor="#991112"
        ErrorMessage="Please enter a valid URL" />

As does this (no error message):

<asp:CustomValidator
        ID="validateUrl"
        OnServerValidate="ValidateWebsiteUrl"
        ClientValidationFunction="isUrlValid" 
        ControlToValidate="websiteURLInput"
        ValidationGroup="CommentGroup"
        runat="server" 
        ForeColor="#991112" />
Was it helpful?

Solution

A validator control has a Text and an ErrorMessage property, in normal usage the Text overrules the ErrorMessage, and the content inside the tag is the same as the Text property (like with Label.Text)

The ErrorMessage is used if no Text is specified or when a ValidationSummary is used.

I hope this helps you to find the real problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top