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!

form validation based on "true" field 1

Status
Not open for further replies.

randyman

Programmer
Feb 15, 2001
5
US
I've about had it with this code. I am creating a form that will validate "true" 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 "true" 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 "checkform" function into the "pick" function. I get errors that are stating that the objects in my "checkform" sub-functions are "null or not an object." 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
 


function pick(frm){
if(frm.rad[0].checked == true){

alert(&quot;Checkbox &quot; + 1 + &quot; is checked.\n&quot;);return checkform(this.form)

}

}


I am curious about this piece.This is the intial checker for validation, right - if that checkbox is checked, the code continues - if not, validation stops?

return checkbox(this.form) ? I think you are just using this improperly. The this keyword has no relevance there - it does not refer to the checkbox, so try using:

return checkform(frm);


this - refers to the current object, so we use it only inside the tags which define objects, or in the constructors for 'new' ones, it enables us to indicate internal methods or data memebers.


b2 - benbiddington@surf4nix.com
 
That's what it was! Thanks man! I've been so focused on the objects acting up, I didn't stop to look at WHY they were acting up.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top