我有幾個文本框只包含日期。如果一個文本框有一個日期,並且用戶試圖提交而不在另一個日期文本框中輸入日期,則它們將在提交之前停止。我想要做到這一點的方式是使用以下JavaScript函數:在javascript中獲取控件
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;
}
}
創建文本框和日期填充如下所示。
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"
這裏是驗證
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
我的問題是,JavaScript是沒有找到兩個文本框,因爲我在代碼 任何想法創造呢?
我的錯誤仍然是一樣的。名稱'txtdate1'未被聲明。 – Eric 2009-08-12 13:14:52