Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Infinite loop checking blank fields with onBlur 2

Status
Not open for further replies.

GMoyle

Programmer
Jul 13, 2001
7
GB
I have a function as follows:

function checkComplete(checkField)
{
alert(checkField.value);
if(checkField.value=="")
{
alert("You have not completed the field");
checkField.focus();
checkField.select();
return false;
}
else
{
return true;
}
}

Essentially, if the field is blank it tells the user and returns the focus to it. It works fine unless the next field also uses the function as it ends up in an infinite loop swapping focus between the two. The function is called with onBlur(this) and I don't really want to use any other events that will need to be called in the HTML (such as onLostFocus..) as I'm writing a set of standard procedures for validation that I want to be as easy to plug in and use as possible.

Any ideas most welcome..
 
<INPUT TYPE=&quot;text&quot; NAME=&quot;testsurname&quot; SIZE=&quot;30&quot; onblur=&quot;checkComplete(this)&quot;>
 
Try using

onblur=&quot;checkComplete(document.formname.testsurname); return true&quot;

instead, substituting the name of your form for &quot;formname&quot;. It's fine to be general with the &quot;this&quot; object when you're doing something that requires the flexibility, but the more specific you are, the less chance there is of problems. I use the same basic code on several commercial sites you have for checkComplete() without a problem, but always send the exact object/element to the function.
 

posting the entire code would be helpful in this situation...i would be interested to see it...perhaps the error would be more obvious.


- spewn
 
Spewn - Thanks for your interest. The full code of the function is in my original posting and it's called with the line in my second posting.

Trollacious - I've tried your idea - Thanks. Unfortunately it hasn't worked (But I'll bear it in mind for future)

Gareth.
 
Try this:
Code:
var busy=0;
function checkComplete(checkField) {
 if (busy) return;
 busy=1;
 if (checkField.value == &quot;&quot;) {
  alert('You haven\'t typed anything in this one!');
  checkField.focus();
  checkField.select();
  setTimeout('busy=0', 1);
 }
 else busy=0;
}


You can edit the validation algorithm however you want. The basic idea is to make it so the function does not execute if it detects that something is already being done. A millisecond after the user blurs the field, it is decided that the function is no longer busy. This is long enough for the next call of the function to recognize that it shouldn't do anything.
Code:
- UNIMENT
 
Uniment - Thanx for your help. That's working fine now.

Gareth.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top