Jesus Zamora
Jesus Zamora

Reputation: 839

Validating Textbox with CustomValidator's ClientValidationFunction not firing

I am having some serious issues with this. I have a two fields like this, both of them being assigned datepickers with jquery.

<asp:TextBox ID="RTRP" runat="server" CssClass="textEntry" Width="120"></asp:TextBox>
<asp:CustomValidator runat="server" ID="CustomValidator3" 
   ControlToValidate="RTRP"
   Text="No date selected" 
   ValidateEmptyText="True"
   ClientValidationFunction="clientValidate" 
   Display="Static">
</asp:CustomValidator>
<asp:TextBox ID="ContEd" runat="server" CssClass="textEntry" Width="120"></asp:TextBox>
<asp:CustomValidator runat="server" ID="CustomValidator1"
    ControlToValidate="ContEd"
    Text="No date selected" 
    ValidateEmptyText="True"
    ClientValidationFunction="clientValidate" 
    Display="Static">
</asp:CustomValidator>

With the following javascript to validate it.

$("#<%=RTRP.ClientID %>").datepicker();
$("#<%=ContEd.ClientID %>").datepicker();

function clientValidate(sender, args) {
    args.IsValid = args.Value.length > 0;
}

Both get their datepickers, but the validation function simply refuses to be fired and always allows the form to submit. I am completely lost here, what am I doing wrong?

Upvotes: 0

Views: 2303

Answers (4)

Kiran1016
Kiran1016

Reputation: 914

Change you method to this.and try again

function clientValidate(sender, args)
{
  if(Page_ClientValidate())
  {
   if (args.Value.length < 0)
   {
    args.IsValid = false;
   } else
   {
    args.IsValid = true;
   }
  }
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

You are checking if a string's length is less than 0 (what is never true) here:

function clientValidate(sender, args) {
    if (args.Value.length < 0) {
        args.IsValid = false;
    } else {
        args.IsValid = true;
    }
}

I'm not sure if this is what you want(you could simpy use a RequiredFieldValidator), but...

function clientValidate(sender, args) {
    args.IsValid = args.Value.length > 0;
}

Upvotes: 1

any user
any user

Reputation: 155

why dont you use

<asp:RequiredFieldValidator id="id" controltovalidate="controlname" erormessage="please enter the dates"> 
</asp:RequiredFieldValidator>

correct me i am wrong

Upvotes: 0

Pratik Bhatt
Pratik Bhatt

Reputation: 911

If you assign $.datepicker() behaviour to any text, and run page.. you will find that textbox on which you override jQuery datepicker had css set display : none.. so that might be the reason that custom validation not getting for that textbox...

Upvotes: 0

Related Questions