views:

339

answers:

4

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");
            }
}
A: 

Hey unknown,

You categorized this question partly as C# yet it seems to be only meant for javascript. Please consider re-categorizing.

Paul Sasik
This should actually be a comment, not an answer!
Chetan Sastry
+2  A: 

In 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);
blesh
+1  A: 

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>
CMS
A: 

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.