PDA

View Full Version : Another Form validation question


danilda
02-11-2003, 06:11 PM
I am trying to make sure that my users click on at least one checkbox. I borrowed some code from another posting, but can't get it to work right. I am a newbie and don't have a clue what I am doing. Any help would be appreciated.
Thanks -- Dee


========================================


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META HTTP-EQUIV="expires" CONTENT="now">

<SCRIPT LANGUAGE="JavaScript">
function checkit(){
var count=0;
var compliment =document.cdc_form.compliment.checked;
var hcs =document.cdc_form.hcs.checked;
var nstdah =document.cdc_form.nstdah.checked;
var questions =document.cdc_form.questions.checked;
var complaint =document.cdc_form.complaint.checked;
var facilities =document.cdc_form.facilities.checked;
var publication =document.cdc_form.publication.checked;
var training =document.cdc_form.training.checked;
var comment =document.cdc_form.comment.checked;
var relay =document.cdc_form.relay.checked;
var booth =document.cdc_form.booth.checked;
var iip =document.cdc_form.iip.checked;
var hardcopy =document.cdc_form.hardcopy.checked;
var memorable =document.cdc_form.memorable.checked;
var clarification =document.cdc_form.clarification.checked;
var topics =document.cdc_form.topics.checked;
var other =document.cdc_form.other.checked;
var memo =document.cdc_form.memo.checked;
var no_response =document.cdc_form.no_response.checked;
var print_copy =document.cdc_form.print_copy.checked;
var supervisor_name =document.cdc_form.supervisor_name.selection;

if (compliment.checked){
count++
}
if (hcs.checked){
count++
}
if (nstdah.checked){
count++
}
if (questions.checked){
count++
}
if (complaint.checked){
count++
}
if (facilities.checked){
count++
}
if (publication.checked){
count++
}
if (training.checked){
count++
}
if (comment.checked){
count++
}
if (relay.checked){
count++
}
if (booth.checked){
count++
}
if (iip.checked){
count++
}
if (hardcopy.checked){
count++
}
if (memorable.checked){
count++
}
if (clarification.checked){
count++
}
if (topics.checked){
count++
}
if (other.checked){
count++
}
if (memo.checked){
count++
}
if (count == 0){
alert("Must choose at least one of the options on the form.")
return false
}
return false
}
</script>

</SCRIPT>

<cfquery name="sups" DATASOURCE="qcr_nah">
SELECT *
FROM supervisors
order by lname, fname
</cfquery>

<head>
<title>CDC NSTDAH QCR FORM</title>
</head>
<body>
<cfset user = replace(#CGI.REMOTE_USER#, "ASHA\","")>
<cfset user = "c:\inetpub\wwwroot\nstdah\files\" & #user# & ".txt">
<cffile action="READ" file="#user#" variable="dee">
<cfscript>
crlf = chr(13);
hcs_name = ListGetAt(dee, 1, crlf);
booth_no = ListGetAt(dee, 2, crlf);
email = ListGetAt(dee, 3, crlf);
</cfscript>
<cfset booth_no = replace(booth_no,"NAH-","")>
<cfset booth_no = replace(booth_no,"NAHSPAN-","")>

<b><center>CDC NSTDAH QCR FORM</center></b><p>

This form will route your questions/comments/requests (QCR) to the appropriate individual(s). Please check the appropriate box(es) and provide as many specifics as possible within the text of your message, including any requested action. When you have completed the form, click “SUBMIT” on the bottom of this screen. This will send your QCR to the HCSII, at the operations desk for processing.</em></strong><p>
<cfform action="cdc_nstdah_qcr_action_form.cfm" method="post" name="cdc_form" enablecab="Yes">
<ol>
<input type="checkbox" name="compliment" value="yes">&nbsp;&nbsp;NSTDAH compliment (general)<br>
<input type="Checkbox" name="hcs" value="yes">&nbsp;&nbsp;HCS Specific Compliment or Complaint (HCS specific)<br>
<input type="checkbox" name="nstdah" value="yes">&nbsp;&nbsp;NSTDAH complaint (general)<br>
<input type="checkbox" name="questions" value="yes">&nbsp;&nbsp;Questions/Comments on Publications<br>
<input type="checkbox" name="complaint" value="yes">&nbsp;&nbsp;Compliment or complaint about NSTDAH referral<br>
<input type="checkbox" name="facilities" value="yes">&nbsp;&nbsp;Memo to Facilities (Building concerns)<br>
<input type="checkbox" name="publication" value="yes">&nbsp;&nbsp;Comment about PSA<br>
<input type="checkbox" name="training" value="yes">&nbsp;&nbsp;Memo to Training Dept<br>
<input type="checkbox" name="comment" value="yes">&nbsp;&nbsp;Comment/question about data collection<br>
<input type="checkbox" name="relay" value="yes">&nbsp;&nbsp;Relay call write-up for NSTDAH TTY Service<br>
<input type="checkbox" name="booth" value="yes">&nbsp;&nbsp;Booth resource problem (missing memos, etc. report computer/phone problems to HCSII)<br>
<input type="checkbox" name="iip" value="yes">&nbsp;&nbsp;IIP update/correction needed (use "CDC/NPIN Database Change Form" for referral updates)<br>
<input type="checkbox" name="hardcopy" value="yes">&nbsp;&nbsp;Hardcopy resource update/correction needed<br>
<input type="checkbox" name="memorable" value="yes">&nbsp;&nbsp;Memorable Call Write Up<br>
<input type="checkbox" name="clarification" value="yes">&nbsp;&nbsp;Information update/clarification needed<br>
<input type="checkbox" name="topics" value="yes">&nbsp;&nbsp;Proposed topics for Team Meetings/Quality Assurance Question/Referral Tip of the Month<br>
<input type="checkbox" name="other" value="yes">&nbsp;&nbsp;Other:&nbsp;&nbsp;<input type="text" name="other_text" size="90"><br>
<input type="checkbox" name="memo" value="yes">&nbsp;&nbsp;Memo to Supervisor/Manager<p>
</ol>

HCS name:&nbsp;&nbsp;<cfoutput>#hcs_name#</cfoutput>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Supervisor name:&nbsp;&nbsp;<cfselect name="supervisor_name" message="test">
<option>
<cfoutput query="sups"><option><!---This is a blank space for the list ---><br>#fname# #lname#<br></option></cfoutput>
</cfselect>
<p>

Booth No:&nbsp;&nbsp;<cfoutput>#booth_no#</cfoutput>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date:&nbsp;&nbsp;<cfoutput>#DateFormat ( Now ( ), "mm/dd/yyyy" )#
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time:&nbsp;&nbsp;#TimeFormat ( Now ( ), "hh:mm tt" )#</cfoutput><p>

<strong>Please check this box if you do not need a response:&nbsp;&nbsp;<input type="checkbox" name="no_response" value="yes"><br>
Please check this box if you would like a printed copy of the QCR.&nbsp;&nbsp;<input type="checkbox" name="print_copy" value="yes"><p></p>
</strong>
<strong>MESSAGE TEXT: <em>(If comment about referral, please include the accession number, name of organization, address, and phone number.)</em></strong><p>
<textarea cols="80" rows="10" name="message_text"></textarea><p>

<input type="hidden" name="hcs_name" value="<cfoutput>#hcs_name#</cfoutput>">
<input type="hidden" name="email" value="<cfoutput>#email#</cfoutput>">
<input type="hidden" name="dater" value="<cfoutput>#DateFormat ( Now ( ), "mm/dd/yyyy" )#</cfoutput>">
<input type="hidden" name="timer" value="<cfoutput>#TimeFormat ( Now ( ), "hh:mm tt" )#</cfoutput>">
<p>
<p>
<input type="submit" name="submit" value="submit" onClick="checkit();">

</cfform>

</body>
</html>

ca_redwards
02-11-2003, 06:55 PM
Since I don't have ColdFusion, I took out all of the CF-proprietary tags, but left in enough that you'll understand what I'm doing. Commentary follows this code:


<head>
<title>CDC NSTDAH QCR FORM</title>
<script type="text/javascript" language="javascript">
function checkit()
{ with(document.cdc_form){chk=[compliment,hcs,nstdah,questions,complaint,facilities,publication,training,comment,relay,booth,iip,ha rdcopy,memorable,clarification,topics,other,memo]};
oneChecked=false;
for(var i=0;i<chk.length;i++)oneChecked|=chk[i].checked;
if(!oneChecked)alert('Please check at least one...');
return oneChecked;
};
</script>
</head>
<body>
<b><center>CDC NSTDAH QCR FORM</center></b><p>

This form will route your questions/comments/requests (QCR) to the appropriate individual(s). Please check the appropriate box(es) and provide as many specifics as possible within the text of your message, including any requested action. When you have completed the form, click “SUBMIT” on the bottom of this screen. This will send your QCR to the HCSII, at the operations desk for processing.</em></strong><p>
<form action="javascript:alert('post data to cdc_nstdah_qcr_action_form.cfm')" method="post" name="cdc_form" enablecab="Yes">
<ol>
<input type="checkbox" name="compliment" value="yes"> NSTDAH compliment (general)<br>
<input type="Checkbox" name="hcs" value="yes"> HCS Specific Compliment or Complaint (HCS specific)<br>
<input type="checkbox" name="nstdah" value="yes"> NSTDAH complaint (general)<br>
<input type="checkbox" name="questions" value="yes"> Questions/Comments on Publications<br>
<input type="checkbox" name="complaint" value="yes"> Compliment or complaint about NSTDAH referral<br>
<input type="checkbox" name="facilities" value="yes"> Memo to Facilities (Building concerns)<br>
<input type="checkbox" name="publication" value="yes"> Comment about PSA<br>
<input type="checkbox" name="training" value="yes"> Memo to Training Dept<br>
<input type="checkbox" name="comment" value="yes"> Comment/question about data collection<br>
<input type="checkbox" name="relay" value="yes"> Relay call write-up for NSTDAH TTY Service<br>
<input type="checkbox" name="booth" value="yes"> Booth resource problem (missing memos, etc. report computer/phone problems to HCSII)<br>
<input type="checkbox" name="iip" value="yes"> IIP update/correction needed (use "CDC/NPIN Database Change Form" for referral updates)<br>
<input type="checkbox" name="hardcopy" value="yes"> Hardcopy resource update/correction needed<br>
<input type="checkbox" name="memorable" value="yes"> Memorable Call Write Up<br>
<input type="checkbox" name="clarification" value="yes"> Information update/clarification needed<br>
<input type="checkbox" name="topics" value="yes"> Proposed topics for Team Meetings/Quality Assurance Question/Referral Tip of the Month<br>
<input type="checkbox" name="other" value="yes"> Other: <input type="text" name="other_text" size="90"><br>
<input type="checkbox" name="memo" value="yes"> Memo to Supervisor/Manager<p>
</ol>

HCS name: <input> Supervisor name: <select name="supervisor_name" message="test">
<option>
<option><!---This is a blank space for the list ---><br>#fname# #lname#<br></option>
</select>
<p>

Booth No: <input> Date: <input><p>

<strong>Please check this box if you do not need a response: <input type="checkbox" name="no_response" value="yes"><br>
Please check this box if you would like a printed copy of the QCR. <input type="checkbox" name="print_copy" value="yes"><p></p>
</strong>
<strong>MESSAGE TEXT: <em>(If comment about referral, please include the accession number, name of organization, address, and phone number.)</em></strong><p>
<textarea cols="80" rows="10" name="message_text"></textarea><p>

<input type="hidden" name="hcs_name" value="<cfoutput>#hcs_name#</cfoutput>">
<input type="hidden" name="email" value="<cfoutput>#email#</cfoutput>">
<input type="hidden" name="dater" value="<cfoutput>#DateFormat ( Now ( ), "mm/dd/yyyy" )#</cfoutput>">
<input type="hidden" name="timer" value="<cfoutput>#TimeFormat ( Now ( ), "hh:mm tt" )#</cfoutput>">
<p>
<p>
<input type="submit" name="submit" value="submit" onClick="if(!checkit())return false;void(0)">

</form>

</body>
</html>


Basically, I extracted the names of your checkbox fields and grouped them all into a single array (chk). Then I just (for) loop through them or'ing their checked property with a(n initially false) boolean oneChecked. At the end, if none were checked, the boolean remains false. The return boolean is used in the submit button's onclick event to allow/prevent the form's submission. From inside the checkit() function itself, an alert can be issued to inform the user that at least one checked box is required. I changed the form action to a javascript alert to demonstrate when/if the form is submitted under various conditions.

;)

beetle
02-11-2003, 06:55 PM
Oog. Whomever wrote that function sure could learn a thing or two about loops.

Replace your checkit functon with this versionfunction checkit()
{
var count = 0;
var f = document.cdc_form;
var checkboxes = [
'compliment',
'hcs',
'nstdah',
'questions',
'complaint',
'facilities',
'publication',
'training',
'comment',
'relay',
'booth',
'iip',
'hardcopy',
'memorable',
'clarification',
'topics',
'other',
'memo',
'no_response',
'print_copy'
];

for ( var i = 0; ( checkbox = checkboxes[i] ); i++ )
{
if( f.elements[checkbox].checked ) count++;
}

if ( count == 0 )
{
alert( "Must choose at least one of the options on the form." );
return false;
}
return true;
}and change the button that calls it like this<input type="submit" name="submit" value="submit" onClick="return checkit();">

danilda
02-11-2003, 08:16 PM
Thank you both very much. I am going to name my first born after you.

dee

ca_redwards
02-11-2003, 08:18 PM
You're welcome!

Originally posted by danilda
Thank you both very much. I am going to name my first born after you.

dee

whammy
02-12-2003, 01:00 AM
Originally posted by beetle
Oog. Whomever wrote that function sure could learn a thing or two about loops.

Yikes! No kidding. :eek:

I've been learning a lot lately from you guys, and studying C#... but that was just atrocious.

Let's see, instead of looping through every checkbox element I care about, let's name them all specifically, and then check to see if each one is checked, one at a time!

Not my idea of a pleasant afternoon programming... ;)

ca_redwards
02-12-2003, 08:58 AM
whammy paraphrased:
Let's see, instead of looping through every checkbox element I care about, let's name them all specifically, and then check to see if each one is checked, one at a time!

I'm willing to think that the form was to be posted at another developer's page or cgi. I often have to write pages with peculiar identifiers just to make them post properly to other people's pages or scripts. And just as I did in my suggested solution, I often redefine said peculiarly named fields into a nice neat array of form elements for easy validation. :thumbsup:

whammy
02-13-2003, 12:25 AM
They said it wasn't their code anyway, that it was copied from another post - I wasn't trying to belittle anyone's code, just agreeing with beetle that it wasn't the most efficient method to accomplish this task.

My opinion is if someone is trying to use some code someone else has written, and it looks very inefficient, then we should probably provide some better code!

Which beetle did, and you did... so no problem. :thumbsup:

P.S. although from what I've seen and read, cold fusion isn't very efficient anyway. ;)