Field Validation in JSP and Servlets

I have a Servlet which dynamically builds a web page containing a number of input fields using HTML. When the Submit button is pressed my doPost method is correctly called and I can validate the input field values correctly.

However I wish to be able to validate a field's data when the user exits the field rather than when all fields have been filled. Can this be done ? How ? In VB this would be similar to the LostFocus field event.

The validation will need to be done by the Servlet as data from a database may need to be retrieved and the fileds are dynamically created depending on the database.

Thanks.

Sarah.

[663 byte] By [SRushworth] at [2007-9-26 14:02:19]
# 1
should be possible to control and post via javascript!?
CMueller at 2007-7-2 15:15:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> However I wish to be able to validate a field's data

> when the user exits the field rather than when all

> fields have been filled.

> The validation will need to be done by the Servlet as

> data from a database may need to be retrieved and the

> fileds are dynamically created depending on the

> database.

These two requirements conflict with each other. Here's why: Somebody uses their web browser to connect to your server and run the servlet. The servlet generates some HTML and sends it back to the browser. At this point the servlet's job is done, so it passes control back to the browser. The user starts entering data into the fields; the browser can control this process somewhat via Javascript, if you like. But the servlet is not sitting there watching what's happening. So it can't do any editing.

So, if your edits include checking a database on the server, you can't do them until the user clicks on "Submit".

DrClap at 2007-7-2 15:15:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

In the HTML world use the onChange event on each INPUT field.

What we did is to create PresentationAttribute classes that represent each field on the form. This knows the label, attribute name, and edits. These edits were data quality edits like must be a number, must be a string, format etc. These edit are associated to javaScript functions. We used these PresentationAttribute class to build the HTML forms.

gfarris at 2007-7-2 15:15:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Hi Sarah,U can do this.Write the field in the servlet in this way:<input type='text" name="txt" onBlur='javascipt:if(this.value=="")alert("please enter the value");this.focus();'>Hope this will help.atanu
deyatanu at 2007-7-2 15:15:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi atanu

> Hi Sarah,

>

> U can do this.Write the field in the servlet in this

> way:

>

> <input type='text" name="txt"

> onBlur='javascipt:if(this.value=="")alert("please

> enter the value");this.focus();'>

This code can possibly result in an infinite alert loop if there are two text box and the user tries to tab out from the first to the second without filling in the first

> Hope this will help.

>

> atanu

>

shubhrajit_c at 2007-7-2 15:15:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...