Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with form validation based on "true" value 1

Status
Not open for further replies.

randyman

Programmer
Feb 15, 2001
5
US
I am creating a form that will validate certain fields based on if a form element (a particular radio button) has been checked. I have created the code to determine the radio button. I have created the required field. But, I can't figure out how to get the "whichIsIt" function to run the MM_validateForm function. Here is the code I created so far. Any suggestions?

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 3.2 Final//EN&quot;>
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function whichIsIt(frm)

{
if(frm.rad[0].checked == true)
{
alert(&quot;Radio button &quot; + 0 + &quot; is checked.&quot;);
}
}

function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&amp;&amp;parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&amp;&amp;d.all) x=d.all[n]; for (i=0;!x&amp;&amp;i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&amp;&amp;d.layers&amp;&amp;i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x &amp;&amp; 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);
if (val) { nm=val.name; if ((val=val.value)!=&quot;&quot;) {
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 == '');
}
//-->
</script>
</HEAD>

<BODY>

<FORM NAME=&quot;simpleFrm&quot;>
<p>Yes:
<INPUT TYPE=&quot;radio&quot; NAME=&quot;rad&quot;>
No:
<INPUT TYPE=&quot;radio&quot; NAME=&quot;rad&quot;>
Maybe:
<INPUT TYPE=&quot;radio&quot; NAME=&quot;rad&quot;>
<BR>
</p>
<p>
<input type=&quot;text&quot; name=&quot;name&quot;>
</p>
<p> </p>
<p> </p>
<p><BR>
<INPUT TYPE=&quot;submit&quot; VALUE=&quot;Which is checked?&quot; onClick=&quot;whichIsIt(this.form);MM_validateForm('name','','R');return document.MM_returnValue&quot; name=&quot;Submit&quot;>
</p>
<p> </p>
</FORM>

</BODY>
</HTML>

THANKS!

Randyman
 

function whichIsIt(frm){
if(frm.rad[0].checked){
alert(&quot;Radio button &quot; + 0 + &quot; is checked.&quot;);
MM_validateform();
}
}


Is this what you mean?
b2 - benbiddington@surf4nix.com
 
YES!!! My god! I feel like an idiot! Sometimes you just can't see the forest through the trees!

Thank you!
 
Well, I thought it was over. Yes, tagging the Dreamweaver validation script did work in that manner. But, I had to abandon that direction because the script was only a text validator. It did not confirm any other form objects. So, I created a test form with all of the elements I am going to use. Same scenario though.
I am creating a form that will validate &quot;true&quot; fields based on if an initial checkbox was checked at the beginning. If the checkbox is unchecked, the validation does not occur.
I have created the code that can verify &quot;true&quot; values in each of the form elements that I am going to use. I have created a seperate code that loops to check if a certain checkbox was checked. Both work fine when I test them independently.
My problem comes when I put the &quot;checkform&quot; function into the &quot;pick&quot; function. I get errors that are stating that the objects in my &quot;checkform&quot; sub-functions are &quot;null or not an object.&quot; I've defined & redefined these to death. Can someone give me some direction please!!!!

Here is the script:

<HTML>
<HEAD>
<TITLE>test</TITLE>
<SCRIPT language = &quot;JavaScript&quot;>
function pick(frm)
{
if(frm.rad[0].checked == true)
{
alert(&quot;Checkbox &quot; + 1 + &quot; is checked.\n&quot;);return checkform(this.form)
}

}



function validString(str)
{
if (str.length != 0)
return true
else
return false
}


function getSelectvalue(list)
{
var listval = list.options[list.selectedIndex].value
return listval
}

function countchecks(form)
{
var numboxes = 4
var count = 0

for (i = 0; i < numboxes; i++) {
var newbox = eval(&quot;form.box&quot; + i)
if (newbox.checked)
count++
}
return count
}


function ischecked(paytype)
{
for (i = 0; i < paytype.length; i++)
{
if (paytype.checked)
return true
}
return false
}

function checkform(form)
{
if(validString(form.fullname.value) == false)
{
alert(&quot;Please provide us with your full name&quot;)
form.fullname.focus()
return false
}

fee = getSelectvalue(form.memberlist)
if (fee == 0) {
alert(&quot;Please choose a membership category&quot;)
form.memberlist.focus()
return false
}

var numcks = countchecks(form)

if (numcks < 1) {
alert(&quot;Please choose a box&quot;)
return false
}

if (!ischecked(form.payment))
{
alert(&quot;Please choose a payment method&quot;)
return false
}
{
alert(&quot;Data is valid!&quot;)

}


}
</SCRIPT>
</HEAD>
<BODY>
<DIV CLASS=&quot;top&quot;>
<p> </p>
</DIV>
<P> Fill out the form below</P>
<FORM>
<TABLE width=&quot;400&quot;>
<TR>
<TD>Would you like to join?
<p> Yes:
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;rad&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;rad&quot;>
</p>
</TD>
</TR>
<TR>
<TD class=&quot;rght&quot;><I>*Full Name</I></TD>
<TD class=&quot;lft&quot;>
<INPUT type=&quot;text&quot; name=&quot;fullname&quot; size=&quot;30&quot; >
</TD>
</TR>
<TR>
<TD>select</TD>
<TD>*
<SELECT name = &quot;memberlist&quot;>
<OPTION value = 0> Select a membership category
<OPTION value = 100> Family - $100
<OPTION value = 75> Individual - $75
<OPTION value = 50> Senior - $50
<OPTION value = 30> Youth - $30
</SELECT>
</TD>
</TR>
</TABLE>
<BR>
<TABLE WIDTH=600>
<TR>
<TD COLSPAN=2>
<B>Check all that apply.</B> </TD>
</TR>
<TR>
<TD>
<INPUT type= checkbox name = &quot;box0&quot;>
check one<BR>
<INPUT type= checkbox name = &quot;box1&quot;>
check two<BR>
<INPUT type= checkbox name = &quot;box2&quot;>
check three<BR>
<INPUT type= checkbox name = &quot;box3&quot;>
check four<BR>
</TD>
<TD BGCOLOR=&quot;silver&quot;> <B>*Choose your method of payment.</B> <BR>
<INPUT type= radio name = &quot;payment&quot;>
Credit card<BR>
<INPUT type= radio name = &quot;payment&quot; >
Bill me<BR>
</TD>
</TR>
</TABLE>
<BR>
<!--Submit button-->
<INPUT type = &quot;button&quot; value = &quot;Submit Form&quot; onClick = &quot;return pick(this.form)&quot;>
<!--Reset button-->
<INPUT type = &quot;reset&quot; value = &quot;Reset Form&quot;>
    
</FORM>

</BODY>
</HTML>



Thank you! Thank you! Thank you!

Randyman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top