aspose file tools*
The moose likes Struts and the fly likes JavaScript Validation for data in HTML Tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JavaScript Validation for data in HTML Tables" Watch "JavaScript Validation for data in HTML Tables" New topic
Author

JavaScript Validation for data in HTML Tables

Radu Zaharia
Greenhorn

Joined: Aug 01, 2001
Posts: 18
In my project I have to dynamically create a table which consists of a variable number of rows.
Each row has an OK button, a Cancel button, a select, and a number of text controls.
I initially load the table from a database, and then I must be able to add, edit or delete rows from the table (and then from the database).
Prior to submitting the new/modified data to the database, I must validate the data in the row I have added/modified (some of the text controls must contain dates, others must contain numbers, and so on).
In one version of the project, I made the validation on the server side, but in this case, if the validation does not succeed, I must reload the page from the database and I loose the changes made by the user on the modified row, which the client finds unacceptable.
So I think I have to make a client side validation using JavaScript. When for example I press the OK button on the row number 3 (from a total of 10 rows) the task I have is this:
1)determine the modified row is row 3;
2)call the JavaScript validation function, which has to validate the text in all the controls from row 3;
So my questions are:
1)how can I determine I pressed the OK button of the row 3? (all the buttons have the same name=�ok�)
2)how can I access the text in the various text controls? (each text control belongs to an array, so for example the names of the text controls on row 3 are: �dataIn_2�, �dataOut_2�, �id_2�, and so on)
Thanks,
Soto


Radu Zaharia
SCJP 1.4, SCBCD 1.3
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Soto Mayor:
...
In one version of the project, I made the validation on the server side, but in this case, if the validation does not succeed, I must reload the page from the database and I loose the changes made by the user on the modified row, which the client finds unacceptable.

This is based from what I understood..
Can't you use a JavaBean component that *maps* to your fields? if your validation fails in the serverside, you forward to a JSP and it'll make use of the bean, without loosing any information.
this is an example.

So I think I have to make a client side validation using JavaScript.

Quick and simple solution. But, what happens if your client disables JavaScript? you are in trouble... you are not solving the problem, you're covering it ;-)

HTH


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Radu Zaharia
Greenhorn

Joined: Aug 01, 2001
Posts: 18
Thank you Andres, I will take a look at the article!
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30776
    
157

If you want to use javascript, you can make each row a separate form. You still should do some error checking on the server though.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaScript Validation for data in HTML Tables