[Javascript] Programmatically set onchange event for textbox

Glenn E. Lanier, II glenn.lanier at gmail.com
Tue Oct 3 20:56:19 CDT 2006


I have a text box that is rendered as 
 
<label for="txtCardNumber"><span id="lblCardNumber" class="NoError
Required">* Card Number:</span></label>
<input name="txtCardNumber" type="text" maxlength="16" id="txtCardNumber" />

(FYI: generated from 

<label for="txtCardNumber"><asp:label id="lblCardNumber" runat="server">*
Card Number:</asp:label></label>
<asp:textbox id="txtCardNumber" runat="server" maxlength="16"></asp:textbox>

)


My client wants to verify the credit card number entered is an accepted
credit card type based on the first digit of the number, so I thought I
would hook an onchange event to the text box. Since I couldn't get anywhere
using ASP.NET code (ontextchanged) (in a hurry, no time to really spend on
this), I thought I would just add the function to the onchange as the page
loads.

So, I have the following code and when the page loads, I do get the "Textbox
found" message. However, when I type in the text box, I do not get the "Card
Number Typed" alert. Any idea what is wrong with my e.onchange statement? If
not, any idea how to use ASP.NET to do this client-side (please send these
replies off-list, since they are non-JS related).

var e = document.getElementById('txtCardNumber');
if (e)
{
   alert("Textbox found");
   e.onchange = "SelectCardType()";
}
		
function SelectCardType()
{
   alert("Card number typed");
}

Thanks,
Glenn




More information about the Javascript mailing list