How to speed up a local validation process

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
lovasco
Forum Newbie
Posts: 15
Joined: Fri May 17, 2002 4:25 am
Location: Milan - Italy

How to speed up a local validation process

Post by lovasco »

Dear folks,
I have a dynamically-built table form with up to 100 rows. Each row has two labels (a line number and a line description) and four text entry fields, where the user can enter numeric values (blanks are not accepted). The names of the four fields have a common prefix ("val") followed by one letter ("o", "r", "s" and "e" respectively) and the associated line number, so that the php post processing can fetch and handle the variables.
The local JavaScript validation function is the following:

Code: Select all

function formValidator()
{
  for (var i = 0; i < document.form1.elements.length; i++)
  &#123;
    if (document.form1.elements&#1111;i].name.substr(0,3) == "val")
    &#123;
      if (isNaN(document.form1.elements&#1111;i].value) || 
        document.form1.elements&#1111;i].value.substr(0,1) == " ")
      &#123;
        alert("Line " + document.form1.elements&#1111;i].name.substr(4,4) + " not numeric");
        document.form1.elements&#1111;i].focus();
        return false;
      &#125;
    &#125;
  &#125;
&#125;
The validation works fine except that, when the table is large, the time required to scan the whole form becomes excessively long (there are app. 400 elements to scan).
I have circumvented the problem by performing the validation in the php post processing (is_numeric() function) and, in case of error, going back to the form (history.go(-1)) which shows the original values, so that the user can correct the mistake and resubmit the form. This soulition seems faster than the JavaScript-based one.
Do you have any suggestions to further speed up this process?
Thank you,
Luca
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

size does matter

Post by phpScott »

I there any real pressing reason why you need so many elements on one page? If you can break up your page into several smaller pages that will dramatically speed things up.
If I was some one surfing to your page and saw so many things to fill in I would probably hit the back button in fright. 8O

phpScott
lovasco
Forum Newbie
Posts: 15
Joined: Fri May 17, 2002 4:25 am
Location: Milan - Italy

Post by lovasco »

You are right, phpScott.
In fact this is a financial intranet application that performs data entry, computes consolidated balances and generates monthly reports. Every month, a selected number of users is responsible for filling in data on behalf of his own companies. The application has been running for some years in a Client/Server fashion (MS Access, AccessBasic, several IBM DB2s with automatic data replication, "C" language stored procedures).
Now we are migrating to a centralized web-based architecture, replacing the current MS Access + AccessBasic presentation layer with a php-based web site, leaving the DB2 + stored procedures component untouched. Also we need to change the current user's interface as little as possible to minimize impact on users.
Every day, in this migration process, we find some interesting "problems" to solve and we try to find out the best solution to them (this forum is of tremendous help to us).
As to the validation process for large forms (so they are and we cannot split them), both ways (JavaScript and php) are working fine, with some slight differences: JS process is a little slower (much depends on the CPU speed), is totally local and, in case of error, shows exactly the wrong cell (focus); php is slightly faster (much depends on the actual network speed) but requires server's processing and tells the user the line where the (first) wrong cell is (we could also add some JS post processing to focus on the target cell).
We tried the JS solution on PCs with different CPU speed and we found it acceptable (3-4 seconds delay).
Thank you all and I'm sure I will bore you with some other problems in the next future.
Bye,
Luca :)
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

it may just be the nature of the JS beast. i like the idea of using php to do the bulk of the processing and using JS to focus on the error cels in case of error. but thats my opinion... :roll:
Post Reply