miko42
asked on
Drop down menus not being validated by standard MM_validateForm() script
Hi, I'm using the standard Macromedia form validation script (see below) to validate a form that has a mix of drop down menus and text fields. While validation script properly validates text fields, check boxes etc., it does not seem to validate the drop down menus at all.
The Script is:
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max, errors='', args=MM_va lidateForm .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') { num = parseFloat(val);
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 (num<min || max<num) 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 == '');
}
The form code has the shape of:
<form .... onSubmit="MM_validateForm( 'Min Rental Days','','R', ....)" ..>
Where "Min Rental Days" is the name of the field. Some of those field names pertain to drop down menus.
Thanks in advance.
The Script is:
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,
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')!=
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
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 (num<min || max<num) 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 == '');
}
The form code has the shape of:
<form .... onSubmit="MM_validateForm(
Where "Min Rental Days" is the name of the field. Some of those field names pertain to drop down menus.
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Please assume that field names are entered correctly.
Bakr, I tried your script version v4.01 - sorry but it didn't work at all...
Bakr, I tried your script version v4.01 - sorry but it didn't work at all...
ASKER
Bakr the problem lies in the form itself. Cause if I move of the drop down list to a different page and form it works. So I gues I have to go throug the form with a find tooth comb.
Thanks for your assistance Bakr
Thanks for your assistance Bakr
Well, then you have to show us your form code, cause in the following example, javascript works just fine.
<html>
<head>
<script type="text/javascript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&p arent.fram es.length) {
d=parent.frames[n.substrin g(p+1)].do cument; 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.len gth;i++) x=MM_findObj(n,d.layers[i] .document) ;
if(!x && d.getElementById) x=d.getElementById(n); alert("Field name: " + n + "\n\n" + "Return Value:" + x); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max, errors='', args=MM_va lidateForm .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') { num = parseFloat(val);
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 (num<min || max<num) 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 == '');
}
//-->
</script>
</head>
<body>
<form name="form1" onsubmit="MM_validateForm( 'Min Rental Days','','R'); return document.MM_returnValue">
<select name="Min Rental Days">
<option value=""></option>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Or for debugging purpose, try placing the following code just before the "return x;" in MM_findObj() function
alert("Field name: " + n + "\n\n" + "Return Value:" + x);
Kind Regards
Bakr
<html>
<head>
<script type="text/javascript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&p
d=parent.frames[n.substrin
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;
for(i=0;!x&&d.layers&&i<d.
if(!x && d.getElementById) x=d.getElementById(n); alert("Field name: " + n + "\n\n" + "Return Value:" + x); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,
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')!=
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
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 (num<min || max<num) 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 == '');
}
//-->
</script>
</head>
<body>
<form name="form1" onsubmit="MM_validateForm(
<select name="Min Rental Days">
<option value=""></option>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Or for debugging purpose, try placing the following code just before the "return x;" in MM_findObj() function
alert("Field name: " + n + "\n\n" + "Return Value:" + x);
Kind Regards
Bakr
Oh, sorry didnt notice you knew it's from the Form.
Good Luck in finding the problem ;)
Good Luck in finding the problem ;)
ASKER
Bakr since you have assisted in pointing me in the correct direction am happy to award you the 500 points :-)
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&p
d=parent.frames[n.substrin
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;
for(i=0;!x&&d.layers&&i<d.
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
Kind Regards
Bakr