Solved

Drop down menus not being validated by standard MM_validateForm()  script

Posted on 2006-07-23
7
708 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:miko42
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
SysTurn earned 500 total points
ID: 17166020
I guess the problem is in the MM_findObj() function, may be it can not find fields containing spaces in their names.
Try to rename the drop down list to something like Min_Rental_Days

Kind Regards
Bakr
0
 
LVL 6

Expert Comment

by:SysTurn
ID: 17166055
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
0
 

Author Comment

by:miko42
ID: 17166134
Please assume that field names are entered correctly.

Bakr, I tried your script version v4.01 - sorry but it didn't work at all...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:miko42
ID: 17166168
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
0
 
LVL 6

Expert Comment

by:SysTurn
ID: 17166182
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
0
 
LVL 6

Expert Comment

by:SysTurn
ID: 17166188
Oh, sorry didnt notice you knew it's from the Form.

Good Luck in finding the problem ;)
0
 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now