I have 3 textboxes to do a check on the entered date. The code I originally had was for one textbox. Is there a way to pass an ID name to the onChange event
<asp:TextBox ID="txtDate" runat="server" Width="110px" onChange="checkEnteredDate('txtDate')"></asp:TextBox>
function checkEnteredDate(var textBox = new String();) {
var inputDate = document.getElementById(textBox);
//if statement to check for valid date
var formatDate = new Date(inputDate.value);
if (formatDate > TodayDate) {
alert("You cannot select a date later than today.");
inputDate.value = TodayDate.format("MM/dd/yyyy");
}
}
I have 3 textboxes to do a check on the entered date. The code I originally had was for one textbox. Is there a way to pass an ID name to the onChange event
<asp:TextBox ID="txtDate" runat="server" Width="110px" onChange="checkEnteredDate('txtDate')"></asp:TextBox>
function checkEnteredDate(var textBox = new String();) {
var inputDate = document.getElementById(textBox);
//if statement to check for valid date
var formatDate = new Date(inputDate.value);
if (formatDate > TodayDate) {
alert("You cannot select a date later than today.");
inputDate.value = TodayDate.format("MM/dd/yyyy");
}
}
Share
Improve this question
edited Jul 24, 2009 at 18:57
Chetan S
23.8k2 gold badges66 silver badges78 bronze badges
asked Jul 24, 2009 at 18:47
MrMMrM
22.1k31 gold badges116 silver badges142 bronze badges
3 Answers
Reset to default 2In order to pass the Id of the textbox you'll have to do this in your code behind's Page_Load:
txtDate.Attributes["onchange"] = String.Format("checkEnteredDate('{0}');",txtDate.ClientID);
You can pass this as the parameter on the onChange assignment:
<asp:TextBox ID="txtDate" runat="server" Width="110px"
onChange="checkEnteredDate(this.id)"></asp:TextBox>
I actually figured it out. I did the following:
...onChange="checkEnteredDate(this)"...
function checkEnteredDate(inputDate) {
//if statement to check for valid date
var formatDate = new Date(inputDate.value);
if (formatDate > TodayDate) {
alert("You cannot select a date later than today.");
inputDate.value = TodayDate.format("MM/dd/yyyy");
}
}
getElement was messing me up. Thanks for the suggestions, they brought me in the right direction.