Onblur() event cause infinite loop

  • Thread starter Bartosz Wegrzyn
  • Start date
B

Bartosz Wegrzyn

I use onblue event to validate fields in my form.
I do this onblur="return isname()" and so so ...

I have one form with 20 fields.

My problem is that when the focus is for example on the first field of my
form
the "name" field and I click somewher or press tab than I loose focus and my
isname() function is executed. Everything is fine but the focus was change
for next field
lets say "address" and the next function is executed isaddress(). Although
my isname() function tries to change the focus back to name field the other
function is executed.
This cause infinite loop and I cannot access the form anymore.

I know I can use the onchange() event handler but for me it will not work.
I need onblur(). Is this code wrong. Maybe the way I code is bad. Please
help.

THE CODE:


function iscustomerid() {

var string1 = document.form1.customerid.value;

if (string1 == "") {

}
return true;
}


function isname() {

var string2 = document.form1.name.value;

if (string2 == "" ) {
alert("Value is required");
document.form1.name.focus();
document.form1.name.select();
return false;
}
else {
if (string2.split(" ").length < 2) {
alert("Enter a full name");
document.form1.name.focus();

document.form1.name.select();
return false;
}
}
return true;
}


function isaddress() {

var string3 = document.form1.address.value;
if (string3 == "" ) {
alert("Value is required");
document.form1.address.focus();
document.form1.address.select();
return false;
}

return true;
}

function iscity() {

var string4 = document.form1.city.value;

if (string4 == "") {
alert("Value is required");
document.form1.city.focus();
document.form1.city.select();
return false;



}

return true;
}



function isstate() {

var string5 = document.form1.state.value;

if (string5 == "") {

}
return true;
}

function iszipcode() {

var string6 = document.form1.zipcode.value;


if (string6 == "") {
alert("Value is required");
document.form1.zipcode.focus();
document.form1.zipcode.select();
return false;
}
return true;
}

function isemail() {

var string7 = document.form1.email.value;

if (string7 == "") {

}
return true;
}

function isphone() {

var string8 = document.form1.phone.value;

if (string8 == "") {
alert("Value is required");
document.form1.phone.focus();
document.form1.phone.select();
return false; }
return true;
}

function isss() {

var string9 = document.form1.ss.value;

if (string9 == "") {

}
return true;
}

function iscc() {

var string10 = document.form1.cc.value;

if (string10 == "") {

}
return true;
}

function isexp() {

var string11 = document.form1.exp.value;

if (string11 == "") {

}
return true;
}

function isdate() {

var string12 = document.form1.date.value;

if (string12 == "") {
alert("Value is required");
document.form1.date.focus();
document.form1.date.select();
return false;
}
return true;
}

function istime() {

var string13 = document.form1.time.value;

if (string13 == "") {

}
return true;
}

function isservice() {

var string14 = document.form1.service.value;

if (string14 == "") {
alert("Value is required");
document.form1.service.focus();
document.form1.service.select();
return false;
}
return true;
}

function isdue() {

var string15 = document.form1.due.value;

if (string15 == "") {
alert("Value is required");
document.form1.due.focus();
document.form1.due.select();
return false;
}

return true;

}

function isextrawork() {

var string16 = document.form1.extrawork.value;

if (string16 == "") {

}
return true;
}

function isdue2() {

var string17 = document.form1.due2.value;

if (string17.value == "") {

}
return true;
}

function isspecial() {

var string18 = document.form1.special.value;

if (string18 == "") {

}
return true;
}


function iscompany() {

var string19 = document.form1.company.value;

if (string19 == "") {
}
return true;
}


function isinstaller() {

var string20 = document.form1.installer.value;


if (string20 == "") {
}
return true;
}


function check() {

alert('Thank You. Now print the work order');
return true;
}Thats how my validate script looks like:
 
F

Fred Serry

Bartosz Wegrzyn said:
I use onblue event to validate fields in my form.
I do this onblur="return isname()" and so so ...

I have one form with 20 fields.

My problem is that when the focus is for example on the first field of my
form
the "name" field and I click somewher or press tab than I loose focus and my
isname() function is executed. Everything is fine but the focus was change
for next field
lets say "address" and the next function is executed isaddress(). Although
my isname() function tries to change the focus back to name field the other
function is executed.
This cause infinite loop and I cannot access the form anymore.

I know I can use the onchange() event handler but for me it will not work.
I need onblur(). Is this code wrong. Maybe the way I code is bad. Please
help.

THE CODE:

Hi,

If you insist in using onblur you will have to follow a somewhat different
approach. As you have noticed, setting focus to a field that did not
validate triggers an onblur for a field that should not be validated yet.
You will have to suppress the validation for the second field.
One way of solving this is using only one function for validating, passing
the name of the field to be validated, and do validations for that field
only until there is no need to reset focus.

var nowValidatingField='';
function val(nameOfField){
// return directly if validating another field
if (nowValidatingField!=''&&nowValidatingField!=nameOfField) return;
// set global var to suppress other validations
nowValidatingField=nameOfField;
// do validations for each field
if (nameOfField=='field1'){
if (!OK){
alert ('this is wrong')
document.form1.field1.focus();
document.form1.field1.select();
return false
}
}
//etc..


// so all is ok! reset global var to release function for new field.
nowValidatingField='';
return true
}

Fred
 
B

Bartosz Wegrzyn

thanks I will try it
Fred Serry said:
and


Hi,

If you insist in using onblur you will have to follow a somewhat different
approach. As you have noticed, setting focus to a field that did not
validate triggers an onblur for a field that should not be validated yet.
You will have to suppress the validation for the second field.
One way of solving this is using only one function for validating, passing
the name of the field to be validated, and do validations for that field
only until there is no need to reset focus.

var nowValidatingField='';
function val(nameOfField){
// return directly if validating another field
if (nowValidatingField!=''&&nowValidatingField!=nameOfField) return;
// set global var to suppress other validations
nowValidatingField=nameOfField;
// do validations for each field
if (nameOfField=='field1'){
if (!OK){
alert ('this is wrong')
document.form1.field1.focus();
document.form1.field1.select();
return false
}
}
//etc..


// so all is ok! reset global var to release function for new field.
nowValidatingField='';
return true
}

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top