This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Scenario: I have a page with text fields - 3 of these fields work together, meaning if you enter data into one of these fields, then LOSE FOCUS via tab or click out, AJAX fires, goes to the server 'validate' method in the action, and recalculates/resets the other two fields.
The problem is if when you are in one of these fields and change the value but do not lose focus to fire validation, BUT hit the enter key, the submit method fires. When this happens, this field is not validated, and the other two fields are not being recalculated and now contain old calculations.
There is code that is already detecting keyCode = 13, then proceeds to submit the form. Is there a way, first thing inside of the keyCode/13 block, to fire off server validation for this one field, then based on the success of the validation (as if the field has lost focus and validated normally), continue with the submit or return back ?
Or, asked another way, is there a way to force the onchange event / validation of the changed field BEFORE the submit occurs, after hitting the enter key ?
You can use onsubmit and check to see if the call was made. Set a flag when the Ajax call is made and when it comes back. [Two states so you know that it is active and completed.] If active have it wait to submit, if it is complete submit, if neither, make the Ajax call and submit after the call comes back.
In relatity I would not rely on the Ajax call I would do the calulation on the server, especially if the user is NOT supposed to redo those numbers. Anyone with a little knowledge can change the fileds if they are protected.