Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript validation...avoiding infinite loop

Posted on 2000-03-31
4
Medium Priority
?
1,111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

721 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