Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] form validation script
- From: dlevy at lancer-ins.com (David S Levy)
- Subject: [Javascript] form validation script
- Date: Fri Mar 28 09:13:13 2003
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 == '');
}
//-->
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.LaTech.edu/mailman/private/javascript/attachments/20030328/b1e1af26/attachment.html
- Follow-Ups:
- [Javascript] form validation script
- From: David T. Lovering
- [Javascript] form validation script
- From: Rodney Myers
- [Javascript] form validation script
- Prev by Date: [Javascript] required entry field
- Next by Date: [Javascript] Flash Detection
- Previous by thread: [Javascript] Flash Detection
- Next by thread: [Javascript] form validation script
- Index(es):