View Full Version : form validation modification

06-16-2004, 07:47 PM
Hey. I'm using a really nifty form validator, but i want the cursor to go back into the first form field that isn't valid

i wanted to incorporate something like -- onError (if e-mail was the first incorrect field:

into the script below, but dont know how.

this goes in the head:

function MM_findObj(n, d) { //v4.01
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 && d.getElementById) x=d.getElementById(n); return x;

function YY_checkform() {

var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
for (i=1; i<a.length;i=i+4){
if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
if (o.type=='text'||o.type=='password'||o.type=='hidden'){
if (r&&v.length==0){err=true}
if (v.length>0)
if (t==1){ //fromto
ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
} else if (t==2){
rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(v))err=true;
} else if (t==3){ // date
dte=new Date(cy,cm,cd);
} else if (t==4){ // time
} else if (t==5){ // check this 2
} else if (t==6){ // the same
} else
if (!o.type&&o.length>0&&o[0].type=='radio'){
at = a[i].match(/(.*)\[(\d+)\].*/i);
if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
if (t==2){
if(!oo){s+='* '+a[i+3]+'\n'}
} else if (o.type=='checkbox'){
} else if (o.type=='select-one'||o.type=='select-multiple'){
}else if (o.type=='textarea'){
if (err){s+='* '+a[i+3]+'\n'; err=false}
if (s!=''){alert('Before the quote request may be submitted, we request that you:\t\t\t\t\t\n\n'+s)}
document.MM_returnValue = (s=='');

and this is the form header

<form action=../../cgi-bin/TFmail.pl method=post name="productchoice" onSubmit="YY_checkform('productchoice','Producer','#q','1','Please select a company','Product','#q','1','Please select a product','Quantity','#1_1000000','1','Please enter the quantity of units you wish to be quoted on \(or on estimate thereof\)','Company','#q','0','Please enter your company name','FirstName','#q','0','Please enter your first name','LastName','#q','0','Please enter your last name','Address','#q','0','Please enter your company\'s address','City','#q','0','Please enter the city in which your company resides','Province','#q','0','Please enter the province in which your company resides','PostalCode','#q','0','Please enter your company\'s postal / zip code','email','#S','2','Please enter a valid e-mail address','Telephone','#q','0','Please enter a telephone number at which you may be contacted','Application_Information','1','1','Please enter information regarding the intended use of the chosen product');return document.MM_returnValue">

how do i do this? or can i?

06-18-2004, 01:13 PM
hm... rather intricate those MM code lines... :D

Try insert the red colored

if (s!=''){alert('Before the quote request may be submitted, we request that you:\t\t\t\t\t\n\n'+s);o.focus()}
document.MM_returnValue = (s=='');

Willy Duitt
06-18-2004, 01:31 PM
hm... rather intricate those MM code lines... :D


Yeppers, there's alot to be said of trying to read mickey mouse code with no formating and non-intuitive variable names. ;)

06-18-2004, 01:49 PM
mickey mouse code

:D This is a great one! I'll remember it to use it some other time :D

Willy Duitt
06-18-2004, 02:13 PM
The developers of Dreamweaver had a sense of humour.

MM == Mickey Mouse :D

06-18-2004, 04:03 PM
i agree the code / formatting thereof is garbage. it was spit out by a dreamweaver extension for form validation.

i take no credit / blame for it's poor construction :D

i wonder what YY stands for then :)

06-18-2004, 04:12 PM
okay... we're on the right track. that set the focus to the last field on the form after it validates... now all thats needed is to get it to set it to the first caught error.

i suppose we could create a variable, and have it set it to 1 the first time it catches an invalid entry in a field, then have the focus set to the field with variable = 1 (or true, or whatever)

would this be too complicated to do?

i can paste the entire form if you guys want..

06-18-2004, 06:24 PM
well I confess that I was not able to follow the entire mickey mouse :D code in amount, but I supposed that the general ideea was to give a return false if the first text field is found not filled in the right mode. Thus, I presumed, the rest of the code will have not to be passed, so it should be the first text field with problems to be focused each time...

Wouldn't be easier to build another "lighter" script for?

06-18-2004, 06:45 PM
i like your approach :)

now.. hmm, how to code it...