Solved

Javascript validation...avoiding infinite loop

Posted on 2000-03-31
4
972 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Problem "Can you help me recover my changes?  I double-clicked the attachment, made changes, and then hit Save before closing it.  But when I try to re-open it, my changes are missing!"    Solution This solution opens the Outlook Secure Temp Fold…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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