Solved

Javascript validation...avoiding infinite loop

Posted on 2000-03-31
4
964 Views
Last Modified: 2013-12-18
I have a several fields on a form and when I tab out of each field I use javascript to validate the field.  If there is an error then I set the focus back to that field.  Works great... unless...

-You tab out of FIELD1 without filling it in and you are brought to FIELD2.  
-The OnBlur event validates FIELD1 when you exit and discovers you have not typed a value so it puts you back in FIELD1.  
-When you are put back in FIELD1 the OnBlur event of FIELD2 is triggered and it discovers I haven't entered a value for FIELD2.  It then tries to put focus back on FIELD2.  

This is an infinite loop...

Is there an easy workaround to avoid this?
0
Comment
Question by:snocross
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:bennyliaw
ID: 2675789
Create a flag variable (could be a hidden field or just a simple javascript variable) that will be set true to indicate that you are validating a field. If the flag is turn on you should never validate another field.

So in your javascript validation code in onBlur event of the fields, you will need to check the flag:

if (!flag) {
   flag = true;
   // your validation goes here
   // if (field1 is wrong)
   //     set focus to field 1
   flag = false;
}

You need to initialized the flag to false upon loading the form

---benny
0
 
LVL 5

Author Comment

by:snocross
ID: 2680252
I tried this and here's what happens;  it finds a validation error and prompts me to fix it.  Then when I fix the error and tab out of the field it doesn't recheck the validation ever again.  How can I reset the flag once the validation error has been fixed?
0
 
LVL 1

Accepted Solution

by:
bennyliaw earned 50 total points
ID: 2680619
Sorry, I thought a boolean flag would work.

Here's the revision that has been tested, flag is now defined as the "field being validated".
In your validation script you will now check whether the current field is the one being validated. (Note you will need to initiazed the flag with "")

function checkField1() {
  if (flag == "Field1" | flag == "") {
    if (document.forms[0].Field1.value == "")
    {  
      alert("Please enter a value in Field1");
      flag = "Field1";
      document.forms[0].Field1.focus();
    }
    else
      flag = ""
  }
}

function checkField2() {
  if (flag == "Field2" | flag == "") {
    if (document.forms[0].Field2.value == "")
    {  
      alert("Please enter a value in Field2");
      flag = "Field2";
      document.forms[0].Field2.focus();
    }
    else
      flag = ""
  }
}

0
 
LVL 5

Author Comment

by:snocross
ID: 2681568
Works great!  Thanks!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lotus Notes and VMWare require a double-click 22 1,398
How to change default font size and font in Lotus Notes 9 6,838
Lotus notes error message 6 100
Text over two lines in VBA for email 26 162
For users on the Lotus Notes 8 Standard client, this article provides information on checking the Java Heap size and adjusting it to half of your system RAM in attempt to get the Lotus Notes 8.x Standard client to run faster.  I've had to exercise t…
Lack of Storage capacity is a common problem that exists in every field of life. Here we are taking the case of Lotus Notes Emails, as we all know that we are totally depend on e-communication i.e. Emails. This article is fully dedicated to resolvin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question