Arrgh.. Insane looping for focus()

Giganews Newsgroups
Subject: Arrgh.. Insane looping for focus()
Posted by:  furry_alarm_clo…@hotmail.com (Craig)
Date: 29 Jul 2003

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&&parent.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.forms.length;i++)
x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n);
return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  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.
';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.
';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a
number between '+min+' and '+max+'.
';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is
required.
'; }
  } if (errors) alert('The following error(s) occurred:
'+errors);
  document.MM_returnValue = (errors == '');
  document.forms['form0'][nm].focus();    
}

    <form name="form0" method="post" action="palm-beta.asp?action=add">
    <table class="table_content" border="0" cellpadding="3"
cellspacing="0">
        <tr class="box_left"><td><b>First Name</b></td></tr>
        <tr class="box_right"><td><input type="textbox" name="First_Name"
maxlength="20" size="21"
onBlur="MM_validateForm('First_Name','','R');return
document.MM_returnValue"></td></tr>
        <tr class="box_left"><td><b>Last Name</b></td></tr>
        <tr class="box_right"><td><input type="text" Name="Last_Name"
maxlength="20" size="21"
onBlur="MM_validateForm('Last_Name','','R');return
document.MM_returnValue"></td></tr>
        <tr class="box_left"><td><b>Email Address</b></td></tr>
        <tr class="box_right"><td><input type="text" Name="Email_Address"
maxlength="50" size="51"
onBlur="MM_validateForm('Email_Address','','RisEmail');return
document.MM_returnValue"></td></tr>
        <tr class="box_info"><td>&nbsp;</td></tr>    
        <tr class="box_info"><td><input type="submit" name="addemail"
value="Submit" class="button"></td></tr>
        </table>
    </form>

Replies