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

7

75

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" />
Hyatt answered 31/3, 2009 at 13:44 Comment(0)
P
115

Your CustomValidator will only fire when the TextBox isn't empty.

If you need to ensure that it's not empty then you'll need a RequiredFieldValidator too.

Note: If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

EDIT:

If your CustomValidator specifies the ControlToValidate attribute (and your original example does) then your validation functions will only be called when the control isn't empty.

If you don't specify ControlToValidate then your validation functions will be called every time.

This opens up a second possible solution to the problem. Rather than using a separate RequiredFieldValidator, you could omit the ControlToValidate attribute from the CustomValidator and setup your validation functions to do something like this:

Client Side code (Javascript):

function TextBoxDCountyClient(sender, args) {
    var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
    if (v == '') {
        args.IsValid = false;  // field is empty
    }
    else {
        // do your other validation tests here...
    }
}

Server side code (C#):

protected void TextBoxDTownCity_Validate(
    object source, ServerValidateEventArgs args)
{
    string v = TextBoxDTownCity.Text;
    if (v == string.Empty)
    {
        args.IsValid = false;  // field is empty
    }
    else
    {
        // do your other validation tests here...
    }
}
Polluted answered 31/3, 2009 at 14:2 Comment(6)
too quick for me, I was typing same answer :) +1Orthotropic
"Your CustomValidator will only fire when the TextBox isn't empty" I tested this on a clean page and it is not true. The client validation function fires even when the textbox is emptyHyatt
My bad. I see what i need to do. If I omit the ControlToValidate property then I succeed in what I want to do, and the client side function fires when I need.Hyatt
This is a cool fix and worked for me. However, I use the ValidatorCalloutExtender from the Ajax Control Toolkit and it requires the ControlToValidate property to be set. It's always something... :-(Rear
Dont forget to specify your javascript script tag correctly, otherwise the validation method is not triggered. (Only if you use an ugly javascript block in your ASPX file.) <script language="javascript" type="text/javascript">Fairleigh
You could also use the CustomValidator.ValidateEmptyText Property? msdn.microsoft.com/en-us/library/…Tajo
R
137

Use this:

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

To validate an empty field.

You don't need to add 2 validators !

Rossen answered 25/8, 2010 at 11:5 Comment(1)
The solution i was searching for. Thanks.Lorenzo
P
115

Your CustomValidator will only fire when the TextBox isn't empty.

If you need to ensure that it's not empty then you'll need a RequiredFieldValidator too.

Note: If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

EDIT:

If your CustomValidator specifies the ControlToValidate attribute (and your original example does) then your validation functions will only be called when the control isn't empty.

If you don't specify ControlToValidate then your validation functions will be called every time.

This opens up a second possible solution to the problem. Rather than using a separate RequiredFieldValidator, you could omit the ControlToValidate attribute from the CustomValidator and setup your validation functions to do something like this:

Client Side code (Javascript):

function TextBoxDCountyClient(sender, args) {
    var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
    if (v == '') {
        args.IsValid = false;  // field is empty
    }
    else {
        // do your other validation tests here...
    }
}

Server side code (C#):

protected void TextBoxDTownCity_Validate(
    object source, ServerValidateEventArgs args)
{
    string v = TextBoxDTownCity.Text;
    if (v == string.Empty)
    {
        args.IsValid = false;  // field is empty
    }
    else
    {
        // do your other validation tests here...
    }
}
Polluted answered 31/3, 2009 at 14:2 Comment(6)
too quick for me, I was typing same answer :) +1Orthotropic
"Your CustomValidator will only fire when the TextBox isn't empty" I tested this on a clean page and it is not true. The client validation function fires even when the textbox is emptyHyatt
My bad. I see what i need to do. If I omit the ControlToValidate property then I succeed in what I want to do, and the client side function fires when I need.Hyatt
This is a cool fix and worked for me. However, I use the ValidatorCalloutExtender from the Ajax Control Toolkit and it requires the ControlToValidate property to be set. It's always something... :-(Rear
Dont forget to specify your javascript script tag correctly, otherwise the validation method is not triggered. (Only if you use an ugly javascript block in your ASPX file.) <script language="javascript" type="text/javascript">Fairleigh
You could also use the CustomValidator.ValidateEmptyText Property? msdn.microsoft.com/en-us/library/…Tajo
C
5

Client-side validation was not being executed at all on my web form and I had no idea why. It turns out the problem was the name of the javascript function was the same as the server control ID.

So you can't do this...

<script>
  function vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="vld" />

But this works:

<script>
  function validate_vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="validate_vld" />

I'm guessing it conflicts with internal .NET Javascript?

Caffey answered 15/4, 2009 at 21:56 Comment(0)
S
4

Also check that you are not using validation groups as that validation wouldnt fire if the validationgroup property was set and not explicitly called via

 Page.Validate({Insert validation group name here});
Scotopia answered 18/5, 2009 at 12:54 Comment(1)
Yes, but you still need text in the textbox for the validator to work anyway!Bitty
F
3

Did you verify that the control causing the post back has CausesValidation set to tru and that it does not have a validation group assigned to it?

I'm not sure what else might cause this behavior.

Frink answered 31/3, 2009 at 13:49 Comment(1)
I use an ImageButton for the Submit, and I have not stated any validation group.Hyatt
P
0

Server-side validation won't fire if client-side validation is invalid, the postback is not send.

Don't you have some other validation that doesn't pass?

The client-side validation is not executed because you specified ClientValidationFunction="TextBoxDTownCityClient" and this will look for a function named TextBoxDTownCityClient as validation function, but the function name should be TextBoxDAddress1Client

(as you wrote)

Phenetole answered 31/3, 2009 at 13:50 Comment(0)
R
0

Thanks for that info on the ControlToValidate LukeH!

What I was trying to do in my code was to only ensure that some text field A has some text in the field when text field B has a particular value. Otherwise, A can be blank or whatever else. Getting rid of the ControlToValidate="A" in my mark up fixed the issue for me.

Cheers!

Resourceful answered 19/1, 2012 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.