Connecting Tech Pros Worldwide Forums | Help | Site Map

Arrgh.. Insane looping for focus()

Craig
Guest
 
Posts: n/a
#1: Jul 20 '05
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_valida teForm.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.\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');retur n
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','','RisEma il');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>

Philip Ronan
Guest
 
Posts: n/a
#2: Jul 20 '05

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@virgin.net
(Please remove the "z"s if replying by email)


Lee
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Arrgh.. Insane looping for focus()


furry_alarm_clock@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

Closed Thread