Onblur() event cause infinite loop

Discussion in 'Javascript' started by Bartosz Wegrzyn, Sep 7, 2003.

  1. 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:
    Bartosz Wegrzyn, Sep 7, 2003
    #1
    1. Advertising

  2. Bartosz Wegrzyn

    Fred Serry Guest

    "Bartosz Wegrzyn" <> schreef in bericht
    news:uFK6b.21077$...
    > 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!=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
    Fred Serry, Sep 7, 2003
    #2
    1. Advertising

  3. thanks I will try it
    "Fred Serry" <> wrote in message
    news:bjg8ha$hl4mn$-berlin.de...
    >
    > "Bartosz Wegrzyn" <> schreef in bericht
    > news:uFK6b.21077$...
    > > 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!=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
    >
    >
    Bartosz Wegrzyn, Sep 8, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    588
    benben
    Jan 31, 2006
  2. Stu
    Replies:
    11
    Views:
    751
    Mark Hubbart
    Nov 13, 2004
  3. Trans
    Replies:
    3
    Views:
    144
  4. Trans
    Replies:
    3
    Views:
    117
    Robert Dober
    Jul 17, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    350
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page