I have a couple of textboxes that will contain dates only. If one textbox has a date and the user tries to submit without entering a date in the other date textbox they will be stopped before submit. The way I want to do this is with the following javascript function:
function ClientValidate(sender, args) {
// Get Both form fields
var txtdate1 = document.getElementById('<%=txtdate1.ClientID%>');
var txtdate2 = document.getElementById('<%=txtdate2.ClientID %>');
// do you client side check to make sure they have something
if (txtdate1.value != '' && txtdate2.value == '') {
args.IsValid = false;
}
else
{
args.IsValid = true;
}
if (txtdate2.value != '' && txtdate1.value == '') {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
The creation of the textboxes and date stuff is as follows.
Dim bttndate1 As New ImageButton
bttndate1.ID = "bttndate1"
Dim txtdate1 As New TextBox
txtdate1.ID = "txtdate1"
txtdate1.Width = 65
Dim calex1 As New AjaxControlToolkit.CalendarExtender
calex1.TargetControlID = "txtdate1"
calex1.Format = "MM/dd/yyyy"
calex1.PopupButtonID = "bttndate1"
'**************** date box2 ***************
Dim bttndate2 As New ImageButton
bttndate2.ID = "bttndate2"
bttndate2.Style.Add("cursor", "pointer")
Dim txtdate2 As New TextBox
txtdate2.ID = "txtdate2"
txtdate2.Width = 65
Dim calex2 As New AjaxControlToolkit.CalendarExtender
calex2.TargetControlID = "txtdate2"
calex2.Format = "MM/dd/yyyy"
calex2.PopupButtonID = "bttndate2"
Here is the Validator
Dim custval As New CustomValidator
custval.ID = "ValidPage"
custval.ClientValidationFunction = "ClientValidate"
custval.ErrorMessage = "You Must Enter a 'From' Date and a 'To' Date"
custval.ErrorMessage = "You Must Select a Vendor"
custval.SetFocusOnError = True
custval.ControlToValidate = "txtdate1"
custval.EnableClientScript = True
MY problem is that the javascript isn't finding the two textboxes because i create them in code any ideas?