Arrgh.. Insane looping for focus()

C

Craig

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[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers.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);
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.substring(8,p); max=test.substring(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'+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>
 
P

Philip Ronan

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 !!!!

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
(e-mail address removed)
(Please remove the "z"s if replying by email)
 
L

Lee

(e-mail address removed) said:
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 !!!!

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
 

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,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top