Link to home
Start Free TrialLog in
Avatar of miko42
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_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.\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.
ASKER CERTIFIED SOLUTION
Avatar of SysTurn
SysTurn
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just tried the script MM_findObj() (version 4.1) and it works fine, try to replace yours (which is version 4.0) with this one:

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;
}

Kind Regards
Bakr
Avatar of miko42
miko42

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...
Avatar of miko42

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
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&&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); 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_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.\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
Oh, sorry didnt notice you knew it's from the Form.

Good Luck in finding the problem ;)
Avatar of miko42

ASKER

Bakr since you have assisted in pointing me in the correct direction am happy to award you the 500 points :-)