Solved

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

Posted on 2006-07-23
7
714 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

829 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