...

View Full Version : Form Validation...help please



tcollick
05-07-2004, 12:34 PM
Hi all,

We've had the form below which includes validation for regular fields and email on our website for a while, but recently added the menu box (in red). However, I don't know how to make it a required field like the others. By this I mean the user would have to choose an option other than "Please Choose". Any help would be appreciated:thumbsup:

<script language="JavaScript" 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); 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>

<form action="http://www.fxadvance.com/register.php" method="POST">
<TABLE WIDTH="73%" align="left" BGCOLOR="FFFFFF" CELLSPACING="1" CELLPADDING="1" BORDER="0">
<br><TR BGCOLOR="FFFFFF">
<TD><FONT CLASS=Verdana-13pxn>Full Name</FONT></TD>
<TD><INPUT NAME="FullName" TYPE="text" size="20">
<BR></TD>
</TR>
<TR BGCOLOR="FFFFFF">
<TD><FONT CLASS=Verdana-13pxn>Phone</FONT></TD>
<TD><INPUT NAME="ContactNumber" TYPE="text" size="20">
<BR></TD>
</TR>
<TR BGCOLOR="FFFFFF">
<TD><FONT CLASS=Verdana-13pxn>Email</FONT></TD>
<TD><INPUT NAME="EmailAddress" TYPE="text" size="35">
<BR></TD>
</TR>
<TR BGCOLOR="FFFFFF">
<TD><FONT CLASS=Verdana-13pxn>Where did you hear about us?</FONT></TD>
<TD><SELECT NAME="Source" TYPE="text" width=50>
<OPTION VALUE="choose" SELECTED>-----Please Choose-----<OPTION VALUE="Yahoo">Yahoo
<OPTION VALUE="MSN">MSN
<OPTION VALUE="FX Labo">FX Labo
<OPTION VALUE="Nikkei Shinbun">Nikkei Shinbun
<OPTION VALUE="CNBC TV">CNBC TV
<OPTION VALUE="Friend">Friend
<OPTION VALUE="Other">Other
</TD></SELECT>
</TR>
<TR BGCOLOR="FFFFFF">
<TD COLSPAN=2><br></TD>
</TR>
<TR BGCOLOR="FFFFFF">
<TD COLSPAN=2><br></TD>
</TR>
<TR BGCOLOR="FFFFFF">
<TD COLSPAN=2 ALIGN="center" valign="top"><INPUT TYPE="submit" onClick="MM_validateForm('FullName','','R','ContactNumber','','R','EmailAddress','','RisEmail');return document.MM_returnValue" VALUE="submit"><INPUT TYPE="reset" VALUE="reset"></TD>
</TR>
</TABLE>
</form>

Spudhead
05-07-2004, 03:42 PM
Well if you've got Dreamweaver then you use whatever inbuilt Dreamweaver button that spawned that validate script, and get it to recreate it with the new form object included. Otherwise you swear a bit, mutter something about Dreamweaver's inbuilt functions being as useful as a waterproof teabag, rip it all out and start writing your own script.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum