Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Onblur() event cause infinite loop

Reply
Thread Tools

Onblur() event cause infinite loop

 
 
Bartosz Wegrzyn
Guest
Posts: n/a
 
      09-07-2003
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:


 
Reply With Quote
 
 
 
 
Fred Serry
Guest
Posts: n/a
 
      09-07-2003

"Bartosz Wegrzyn" <> schreef in bericht
news:uFK6b.21077$ y.com...
> 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:
>
>

<code snipped>
>


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!=nameO fField) 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


 
Reply With Quote
 
 
 
 
Bartosz Wegrzyn
Guest
Posts: n/a
 
      09-08-2003
thanks I will try it
"Fred Serry" <> wrote in message
news:bjg8ha$hl4mn$...
>
> "Bartosz Wegrzyn" <> schreef in bericht
> news:uFK6b.21077$ y.com...
> > 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:
> >
> >

> <code snipped>
> >

>
> 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!=nameO fField) 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Why is this cause an infinite loop? Trans Ruby 3 07-17-2008 05:31 PM
warning: redefining Object#initialize may cause infinite loop Trans Ruby 3 09-10-2006 02:23 PM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
warning: redefining Object#initialize may cause infinite loop Stu Ruby 11 11-13-2004 12:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57