Solved

Javascript validation...avoiding infinite loop

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now