A
Alan Silver
Hello,
I have some simple client-side Javascript validation that was working
fine until now. For some reason (don't ask me), it has stopped working
and I can't figure out why. I have switched to version 2.0 (from 1.1)
and have changed the page to use a master page, but other than that,
it's the same.
I am trying to validate the from date for a credit card to make sure the
date has already passed. The code is pretty straightforward. I have two
dropdown controls and a custom validator...
<asp
ropDownList ID="drpFromMonth" RunAt="server">
<asp:ListItem value="01">01/January</asp:ListItem>
.... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp
ropDownList>
<asp
ropDownList ID="drpFromYear" RunAt="server" />
<asp:CustomValidator ControlToValidate="drpFromMonth"
OnServerValidate="ValidateValidFrom"
ClientValidationFunction="ValidateValidFromDate" Display="Dynamic"
ErrorMessage="not valid yet" Text="not valid yet" Runat="Server" />
The values for drpFromYear are set in code. The client-side function
looks like this...
function ValidateValidFromDate(s, e) {
fromMonth = parseInt(document.aspnetForm.drpFromMonth.selectedIndex);
fromYear = parseInt(document.aspnetForm.drpFromYear.options[document.aspnetForm.drpFromYear.selectedIndex].value);
now = new Date;
nowMonth = now.getMonth();
nowYear = now.getYear();
dateOK = false;
if (fromYear > nowYear) {
dateOK = false;
}
if (fromYear == nowYear) {
if (fromMonth <= nowMonth) {
dateOK = true;
} else {
dateOK = false;
}
}
if (fromYear < nowYear) {
dateOK = true;
}
e.IsValid = dateOK;
}
I originally had document.form1.drpFromMonth in there, which worked.
Since I had this error, I tried changing it to
document.aspnetForm.drpFromMonth as that uses the form name that is
generated by the framework. I tried setting the form's id myself, but it
was ignored. Whatever I did, the generated page had aspnetForm as the
form name and ID. I changed my client-side code to use this, but it
still didn't work.
When I try the page, I get an error...
document.aspnetForm.drpFromMonth.selectedIndex is null or not an object
I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMonth"], but with the same result.
Anyone any idea how to get this to work? TIA
I have some simple client-side Javascript validation that was working
fine until now. For some reason (don't ask me), it has stopped working
and I can't figure out why. I have switched to version 2.0 (from 1.1)
and have changed the page to use a master page, but other than that,
it's the same.
I am trying to validate the from date for a credit card to make sure the
date has already passed. The code is pretty straightforward. I have two
dropdown controls and a custom validator...
<asp
<asp:ListItem value="01">01/January</asp:ListItem>
.... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp
<asp
<asp:CustomValidator ControlToValidate="drpFromMonth"
OnServerValidate="ValidateValidFrom"
ClientValidationFunction="ValidateValidFromDate" Display="Dynamic"
ErrorMessage="not valid yet" Text="not valid yet" Runat="Server" />
The values for drpFromYear are set in code. The client-side function
looks like this...
function ValidateValidFromDate(s, e) {
fromMonth = parseInt(document.aspnetForm.drpFromMonth.selectedIndex);
fromYear = parseInt(document.aspnetForm.drpFromYear.options[document.aspnetForm.drpFromYear.selectedIndex].value);
now = new Date;
nowMonth = now.getMonth();
nowYear = now.getYear();
dateOK = false;
if (fromYear > nowYear) {
dateOK = false;
}
if (fromYear == nowYear) {
if (fromMonth <= nowMonth) {
dateOK = true;
} else {
dateOK = false;
}
}
if (fromYear < nowYear) {
dateOK = true;
}
e.IsValid = dateOK;
}
I originally had document.form1.drpFromMonth in there, which worked.
Since I had this error, I tried changing it to
document.aspnetForm.drpFromMonth as that uses the form name that is
generated by the framework. I tried setting the form's id myself, but it
was ignored. Whatever I did, the generated page had aspnetForm as the
form name and ID. I changed my client-side code to use this, but it
still didn't work.
When I try the page, I get an error...
document.aspnetForm.drpFromMonth.selectedIndex is null or not an object
I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMonth"], but with the same result.
Anyone any idea how to get this to work? TIA