ASP.NET Custom Validator Client side & Server Side validation not firing

REA_ANDREW picture REA_ANDREW · Mar 31, 2009 · Viewed 250.3k times · Source

This has not happened to me before, but for some reason both the client and server side validation events are not being triggered:

<asp:TextBox ID="TextBoxDTownCity" runat="server" CssClass="contactfield" />
<asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="true"
    ErrorMessage="Delivery Town or City required"
    ClientValidationFunction="TextBoxDTownCityClient" 
    ControlToValidate="TextBoxDTownCity"
    OnServerValidate="TextBoxDTownCity_Validate" Display="Dynamic" >
</asp:CustomValidator>

Server-side validation event:

protected void TextBoxDTownCity_Validate(object source, ServerValidateEventArgs args)
{
    args.IsValid = false;
}

Client-side validation event:

function TextBoxDCountyClient(sender, args) {
    args.IsValid = false;
    alert("test");
}

I thought at the least the Server Side validation would fire but no. this has never happened to me before. This has really got me stumped.

I looked at the output and ASP.NET is recognizing the client side function:

ASP.NET JavaScript output:

var ctl00_ctl00_content_content_CustomValidator2 = document.all ? document.all["ctl00_ctl00_content_content_CustomValidator2"] : document.getElementById("ctl00_ctl00_content_content_CustomValidator2");

ctl00_ctl00_content_content_CustomValidator2.controltovalidate = "ctl00_ctl00_content_content_TextBoxDTownCity";

ctl00_ctl00_content_content_CustomValidator2.errormessage = "Delivery Town or City required";

ctl00_ctl00_content_content_CustomValidator2.display = "Dynamic";

ctl00_ctl00_content_content_CustomValidator2.evaluationfunction = "CustomValidatorEvaluateIsValid";

ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction = "TextBoxDTownCityClient";

Rendered custom validator:

<span id="ctl00_ctl00_content_content_CustomValidator2" style="color:Red;display:none;">Delivery Town or City required</span> 

Can any one shed some light as to why both client and server side validation would not be firing.

Edit: Typo I pasted in the wrong function, problem still the same

Just another update to the last comment: where by the TextBox cannot be empty. I tested this out and it is not true. On a blank page the CustomValidator fired my client side validation function fine without a value:

<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ErrorMessage="CustomValidator" ClientValidationFunction="TextBoxDAddress1Client"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Answer

Carl Nielsen picture Carl Nielsen · Aug 25, 2010

Use this:

<asp:CustomValidator runat="server" id="vld" ValidateEmptyText="true"/>

To validate an empty field.

You don't need to add 2 validators !