0

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?

0

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Tried that, but it still does not seem to trigger at all. Even tried to call an alert and changing the text in another field, but apparently it is skipping it completely.
@JesusZamora: Your TextBoxes don't have AutoPostBack="true"(default is false), maybe you want to postback immediately.
@JesusZamora: But where's the submit button you use?
It's within the same asp:Content as both textboxes.
0

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...

Comments

0

why dont you use

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

correct me i am wrong

1 Comment

Yes, that's what I would have used, but later on I need it to be able to check if a checkbox is checked in order to proceed. Thanks anyway.
0

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;
   }
  }
}

1 Comment

clientValidate is already the event handler, this would cause an infinite loop( imho ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.