• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to validate text fields after enter key press

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


Eric
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic