Can you prevent a JavaScript event in the queue from being executed by another event?
Situation:
I have two ASP.Net controls,
Age - TextBox
Save - Button
There are two JavaScript validation functions,
ValidateAge()
- verifies if the age is between 0 and 140, shows an alert if invalid
ValidatePage()
- checks that all required fields are filled in before saving
<asp:TextBox ID="txtAge" TabIndex="1" DataType = "String" runat="server" MaxLength="50" CssClass="textBox" Style="width: 150px" CausesValidation="true" onblur="return ValidateAge();"></asp:TextBox>
and a specific access key for the button,
<asp:Button ID="btnSave" AccessKey="S" AssociatedControlID="btnSave" TabIndex="1" runat="server" CssClass="ButtonSaveNew" onclick="return ValidatePage();"></asp:Button>
When I press Alt+S with an invalid age in the Age field, first the onblur
of the Age field is triggered (as I've set the AssociatedControlID
), followed by the onclick
of the Save button.
However, regardless of the age's validity, the save action is executed.
I want to be able to stop the execution of the onclick
event of the button if the age is invalid in the onblur
event of the textbox.