i have 4 text boxes searching 2 different date columns of table. (the user can put range each column). using date optional, because of created 4 customvalidators use same clientvalidationfunction. (i can't use range or required validator because optional). here custom function:
function validatedaterange(s, args) { var startdate; var enddate; var contovalsplit = document.getelementbyid(s.controltovalidate).id.split("_"); var controltovalidate = contovalsplit[contovalsplit.length - 1]; switch (controltovalidate) { case "txtfirststartdate": startdate = document.getelementbyid("<%= txtfirststartdate.clientid %>").value; enddate = args.value; break; case "txtsecondstartdate": startdate = args.value; enddate = document.getelementbyid("<%= txtsecondstartdate.clientid %>").value; break; case "txtsecondenddate": startdate = document.getelementbyid("<%= txtsecondenddate.clientid %>").value; enddate = args.value; break; default: startdate = args.value; enddate = document.getelementbyid("<%= txtfirstenddate.clientid %>").value; break; } if (new date(args.value) == "invalid date"){ $(s).text("please enter valid end date(mm/dd/yyyy)."); args.isvalid = false; return; } if (enddate != '') { if (new date(startdate) > new date(enddate)) { $(s).text("end date must after start date."); args.isvalid = false; return; } args.isvalid = true; return; }
here textbox , customvalidators:
<td class="startdatelabel">date from</td> <td> <asp:textbox runat="server" cssclass="startdate" id="txtfirstartdate" onblur="displayinvalidlabel"/> <asp:customvalidator id="cusvalfromdate" runat="server" errormessage="end date must after start date." clientvalidationfunction="validatedaterange" controltovalidate="txtfirstartdate" enableclientscript="true" setfocusonerror="true" validationgroup=""searchgroup" cssclass=""validator" display="dynamic"> </asp:customvalidator> <span class="to label"> </span> <asp:textbox runat="server" cssclass="enddate" id="txtfirstenddate" onblur="displayinvalidlabel"/> <asp:customvalidator id="cusvaltodate" runat="server" errormessage="end date must after start date." clientvalidationfunction="validatedaterange" controltovalidate="txtfirstenddate" enableclientscript="true" setfocusonerror="true" validationgroup="searchgroup" cssclass="validator" display="dynamic"> </asp:customvalidator> </td> <td class="startdatelabel">second date from</td> <td> <asp:textbox runat="server" cssclass="startdate" id="txtsecondstartstart" tooltip="this field allows ranges. see legend" onblur="displayinvalidlabel()"/> <asp:customvalidator id="custsecstartval" runat="server" errormessage="end date must after start date." clientvalidationfunction="validatedaterange" controltovalidate="txtsecondstartstart" enableclientscript="true" setfocusonerror="true" validationgroup="searchgroup" cssclass="validator" display="dynamic"> </asp:customvalidator> </td> <td class="to label">second to</td> <td> <asp:textbox runat="server" cssclass="enddate" id="txtsecondenddate" tooltip="this field allows ranges. see legend" onblur="displayinvalidlabel()"/> <asp:customvalidator id="custsecendval" runat="server" errormessage="end date must after start date." clientvalidationfunction="validatedaterange" controltovalidate="txtsecondenddate" enableclientscript="true" setfocusonerror="true" validationgroup="searchgroup" cssclass="validator" display="dynamic"> </asp:customvalidator> </td>
the validation works fine. when enter invalid value respective error message shows next defined control. focus placed needs be. if tab through 4 entry boxes when tab out of third 1 ("txtsecondstartdate") if don't enter message "txtfirststartdate" disappears invalid date remaining. know resseting validation true missing doing that?
that same text boxes. works if click in other control has validator after couple clicks clears messages of other ones should remain invalid. can't see happeneing?
well, figured out. problem comming "onblur" event have on each input textbox. had tied function validating causing mix when inputs , weren't happening.