In a textbox user can enter date. In OnChange event I am trying to validate the date using javascript, if it is not a valid date showing an alert message and setting the focus on the textbox.
Here when I enter invalid date and press tab, the onchange events fires , in case of invalid date it does not set focus on the texbox, instead the focus is set on next textbox in the form.
<asp:TextBox ID="txtDateOfBirth" runat="server"
onchange="validateDate(this)"></asp:TextBox>
function validateDate(sender) {
if (!Date.parseInvariant(sender.value, "MM/dd/yyyy")) {
alert("date is incorrect");
sender.focus();
}
}
onchangeevent, so the focus is indeed set to the textbox, but it was already there, and then the keypress event occurs moving focus to the next textbox.