I have date fields and I want to validate if both the dates are selected or none. I have added the following customValidator
<asp:CustomValidator ID="CustomValidator3" runat="server" ErrorMessage="CustomValidator" Text="You must select both or no dates" ClientValidationFunction="dateValidate" ValidateEmptyText="false" Font-Size="Small" Font-Bold="True" ForeColor="Red" SetFocusOnError="True"></asp:CustomValidator>
But it does not work if I am not adding customvalidator. My client side function is below. This method works fine otherwise when I am validating in date fields directly but I am trying to implement it using customvalidator.
function dateValidate(sender, args) {
var From = document.getElementById('dataContentplaceholder_wdpFrom').title;
var To = document.getElementById('dataContentplaceholder_wdpTo').title;
if (From.toString.length == 0 && To.toString.length >=1 || To.toString.length == 0 && From.toString.length >=1) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
If the date fields are rendered as TextBoxes (I am not familiar with Infragistics), you could use a markup similar to this one:
<asp:TextBox ID="txtBox1" runat="server" onchange="ValidateTexts();" ... />
<asp:TextBox ID="txtBox2" runat="server" onchange="ValidateTexts();" ... />
<asp:CustomValidator ID="customValidator1" runat="server" Text="You must select both or no dates" ForeColor="Red" ClientValidationFunction="txtValidate" ValidateEmptyText="true" ... />
With the following client-code:
function ValidateTexts() {
ValidatorValidate(document.getElementById('<%= customValidator1.ClientID %>'));
}
function txtValidate(sender, args) {
var from = document.getElementById('<%= txtBox1.ClientID %>').value;
var to = document.getElementById('<%= txtBox2.ClientID %>').value;
args.IsValid = (from.length == 0 && to.length == 0) || (to.length > 0 && from.length > 0);
}
The onchange
event handler is called when a modified field loses focus. Without it, the validation is done only when a postback is triggered.