Solved

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

Posted on 2006-07-23
7
702 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:miko42
Comment Utility
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
Comment Utility
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
Comment Utility
Oh, sorry didnt notice you knew it's from the Form.

Good Luck in finding the problem ;)
0
 

Author Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now