File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes How to validate text fields after enter key press Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to validate text fields after enter key press" Watch "How to validate text fields after enter key press" New topic

How to validate text fields after enter key press

ronnie johnson

Joined: Sep 05, 2008
Posts: 1
Hello all, I need some guidance...

Using Struts, jsp, AJAX, java.

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 ?

I hope this is clear enough, I apologize if not as I am new to Javascript altogether.

Many Thanks!
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
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.

I agree. Here's the link:
subject: How to validate text fields after enter key press
It's not a secret anymore!