[Javascript] form validation script

David T. Lovering dlovering at gazos.com
Fri Mar 28 11:17:40 CST 2003


I don't have my dreamweaver handbook handy at present, but I seem to recall the second parameter of the MM_validateForm triplet was a tagName/type descriptor.  If I'm right, this would go a long way towards negating all your problems in one shot.  The
default where the second parameter was null ("") was, if I remember rightly, a generic text box.

Can anybody confirm this?  I'd be interested as well, even though I've limited my use of Dreamweaver to building landing pads for my Flash objects.  (Too many shims/splines, and bogus objects for my taste).

-- Dave Lovering

David S Levy wrote:
> 
>    Part 1.1.1    Type: Plain Text (text/plain)
>              Encoding: quoted-printable
> 
>    Part 1.2    Type: Plain Text (text/plain)
>            Encoding: 7bit
Hi, I'm having trouble with the following two functions.  They were generated by dreamweaver.  The functions work together to validate a form.  MM_validateForm() is called on submitting the form, and in turn calls MM_findObj, which takes a form input name
passed to MM_validate form and returns the input object of that name.  MM_validateForm() takes in parameters in triplets.  For instance, if I had only one required parameter called 'Name', I would call MM_validateForm('Name','','R'), where R is the flag
indicating the field is required.  If I had 2 name fields I wanted to validate it would be 
MM_validateForm('Name1','','R','Name2','','R') and etc, inductively.  I'm not sure what the second parameter in each triplet set is for.  Anyway, there are two problems here.  

The first problem is that if one of the parameters I'm trying to require is a checkbox, the script looks at its value as though it was checked.  IE if the value of the checkbox is 'yes' the script returns a 'yes' regardless of whether the box was checked,
and therefore the field is not made required.  

The second problem is that when I have a radio input the script considers the name and value of it to be undefined all the time.  

I can write a simpler and more proprietary script for this but am pretty confused as to why this one is not working.

Does anyone have any ideas concerning this?

Thanks,
david.

<!--
function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); 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') {
        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 (val<min || max<val) 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 == '');
}
//-->


More information about the Javascript mailing list