ONBLUR() LOOP

Giganews Newsgroups
Subject: ONBLUR() LOOP
Posted by:  Bartosz Wegrzyn (blwegrz…@lexon.ws)
Date: Thu, 04 Sep 2003

I do validate my form using onblur() event.

In my example I have 3 fields in my form.
name,address and city. I call my functions using onblur()="isname(this)"
...etc.  My probllem is that when I fill the form fields everything is
fine. When I dont I have a problem.  When I leave empty field for lets say
name the the isname() function is called and tells my to enter the data.
It tries to focus on that field and select it.  The problem is that when I
change the focus from name to the second field address the second script
is already running.  So when I click OK on my alert message I cant fill in
the field because second field validation function runs.  So I am in the
loop.  WHY?  Is there any way I can stop the computer to focus on next
field when I press tab or click on the next field.  Whats wrong.
Thats my code.

function isname(field) {
  if (field.value == "") {
    alert("Value is required");
    field.focus();
    
    field.select();
  }
  else {
    if (field.value.split(" ").length < 2) {
      alert("Enter a full name");
      field.focus();
    
      field.select();
    }
  }
}

function isaddress(field) {
  if (field.value == "") {
    alert("Value is required");
    field.focus();
    field.select();
  }
}

function iscity(field) {
  if (field.value == "") {
    alert("Value is required");
    field.focus();
    field.select();
  }
}

Thanks a lot

Replies