Arrgh.. Insane looping for focus()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig

    Arrgh.. Insane looping for focus()

    Here is some javascript checking I have for form validation. The
    problem is is that it loops..... How can I make it stop ? IF I click
    on the last name text box, it scolds me, saying the first_name needs
    to be filled out. I click ok, and it says the last_name needs to be
    filled out...i click ok and it says the first_name needs to be
    filled.. wash rinse repeat !!!!

    function MM_findObj(n, d) { //v4.0
    var p,i,x; if(!d) d=document;
    if((p=n.indexOf ("?"))>0&&paren t.frames.length ) {
    d=parent.frames[n.substring(p+1 )].document; n=n.substring(0 ,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.fo rms.length;i++)
    x=d.forms[i][n];
    for(i=0;!x&&d.l ayers&&i<d.laye rs.length;i++)
    x=MM_findObj(n, d.layers[i].document);
    if(!x && document.getEle mentById) x=document.getE lementById(n);
    return x;
    }

    function MM_validateForm () { //v4.0
    var i,p,q,nm,test,n um,min,max,erro rs='',args=MM_v alidateForm.arg uments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2];
    val=MM_findObj( args[i]);
    if (val) { nm=val.name; if ((val=val.value )!="") {
    if (test.indexOf(' isEmail')!=-1) { p=val.indexOf(' @');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain
    an e-mail address.\n';
    } else if (test!='R') {
    if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
    if (test.indexOf(' inRange') != -1) { p=test.indexOf( ':');
    min=test.substr ing(8,p); max=test.substr ing(p+1);
    if (val<min || max<val) errors+='- '+nm+' must contain a
    number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is
    required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+er rors);
    document.MM_ret urnValue = (errors == '');
    document.forms['form0'][nm].focus();
    }

    <form name="form0" method="post" action="palm-beta.asp?action =add">
    <table class="table_co ntent" border="0" cellpadding="3"
    cellspacing="0" >
    <tr class="box_left "><td><b>Fi rst Name</b></td></tr>
    <tr class="box_righ t"><td><inpu t type="textbox" name="First_Nam e"
    maxlength="20" size="21"
    onBlur="MM_vali dateForm('First _Name','','R'); return
    document.MM_ret urnValue"></td></tr>
    <tr class="box_left "><td><b>La st Name</b></td></tr>
    <tr class="box_righ t"><td><inpu t type="text" Name="Last_Name "
    maxlength="20" size="21"
    onBlur="MM_vali dateForm('Last_ Name','','R');r eturn
    document.MM_ret urnValue"></td></tr>
    <tr class="box_left "><td><b>Em ail Address</b></td></tr>
    <tr class="box_righ t"><td><inpu t type="text" Name="Email_Add ress"
    maxlength="50" size="51"
    onBlur="MM_vali dateForm('Email _Address','','R isEmail');retur n
    document.MM_ret urnValue"></td></tr>
    <tr class="box_info "><td>&nbsp ;</td></tr>
    <tr class="box_info "><td><inpu t type="submit" name="addemail"
    value="Submit" class="button"> </td></tr>
    </table>
    </form>
  • Philip Ronan

    #2
    Re: Arrgh.. Insane looping for focus()

    On 03.7.29 5:02 PM, Craig wrote:
    [color=blue]
    > Here is some javascript checking I have for form validation. The
    > problem is is that it loops..... How can I make it stop ? IF I click
    > on the last name text box, it scolds me, saying the first_name needs
    > to be filled out. I click ok, and it says the last_name needs to be
    > filled out...i click ok and it says the first_name needs to be
    > filled.. wash rinse repeat !!!![/color]

    This is the sort of annoying behaviour you get when you use onBlur events to
    validate form input.

    Be nice and use an onSubmit checker instead.

    Philip Ronan
    phil.ronanzzz@v irgin.net
    (Please remove the "z"s if replying by email)


    Comment

    • Lee

      #3
      Re: Arrgh.. Insane looping for focus()

      furry_alarm_clo ck@hotmail.com said:[color=blue]
      >
      >Here is some javascript checking I have for form validation. The
      >problem is is that it loops..... How can I make it stop ? IF I click
      >on the last name text box, it scolds me, saying the first_name needs
      >to be filled out. I click ok, and it says the last_name needs to be
      >filled out...i click ok and it says the first_name needs to be
      >filled.. wash rinse repeat !!!![/color]

      Don't validate onBlur. Use onChange, if you must validate immediately.
      You'll have to validate again onSubmit (and, of course, again on the server).

      100 You click the "last name" box, giving it focus.
      200 Since focus left the "first name" box, the popup message appears.
      300 Then your code sets focus back to the "first name" box.
      400 Since focus left the "last name" box, the popup message appears.
      500 Then your code sets focus back to the "last name" box.
      600 GOTO 200

      Comment

      Working...