[Javascript] Form Validation
Rodney Myers
rodney at aflyingstart.net
Tue Sep 18 16:55:55 CDT 2001
Teresa,
Interesting.
Excuse the critique, but when I look at scripts I make notes. so you may as
well have them:
function GoForm2(form)
So form object is defined in parameter
So why?
if (document.Form2.finished[1].checked)
and not
if (form.finished[1].checked)
for (var e = 0; e < form.elements.length; e++)
This goes right through the form - including the radio buttons at the end which
switch the type of submit, the four hidden fields near the ned and the Submit
button
Could be
for (var e = 0; e < form.elements.length-7; e++)
var el = form.elements[e];
if (el.type == 'radio' && el.type != 'hidden')
{
var group = form[el.name];
This last line flummoxes me? I could understand it if you had sets of elements
with the same name but you (now?) have not (but once did?)
Elements : 34
[##] Type Name Value
[ 0] hidden QNumber1 1
[ 1] hidden Q1ACounter 4
[ 2] hidden QType1 1
[ 3] checkbox Q1A1 CHECKED value =
Erogonomics Training
[ 4] hidden Q1A1B Erogonomics Training
[ 5] checkbox Q1A2 CHECKED value = Health
Issues
[ 6] hidden Q1A2B Health Issues
[ 7] checkbox Q1A3 -OFF- value = Plant
Safety
[ 8] hidden Q1A3B
[ 9] checkbox Q1A4 -OFF- value = Wastwater
Protocols
[10] hidden Q1A4B
[11] hidden QNumber2 2
[12] hidden Q2ACounter 3
[13] hidden QType2 1
[14] checkbox Q2A1 -OFF- value = 1
[15] hidden Q2A1B
[16] checkbox Q2A2 CHECKED value = 2
[17] hidden Q2A2B 2
[18] checkbox Q2A3 -OFF- value = 3
[19] hidden Q2A3B
[20] hidden QNumber3 3
[21] hidden Q3ACounter 2
[22] hidden QType3 1
[23] checkbox Q3A1 -OFF- value = 1
[24] hidden Q3A1B
[25] checkbox Q3A2 -OFF- value = 2
[26] hidden Q3A2B
[27] hidden Survey 266
[28] hidden LID 322111
[29] hidden Count 3
[30] hidden R 4
[31] radio finished -OFF- value = N
[32] radio finished CHECKED value = Y
[33] submit button1 SUBMIT : Save/Submit Survey
I think you would need to make artificial groups searching for prefixes in
names such as "Q1"
I have to say I have not got into the logic of what you are doing here. But it
does seem to have come off the rails a bit when you found you needed unique
names.
Doable, certainly, but now I must do my own work<g>
hth
Rodney
Teresa L Cannon wrote:
> I have a survey where I allow the users to "Save As Incomplete". This
> pulls out current information on questions they have answered and allows
> them to go back and delete/update/insert answers before the final submit.
> In order to do this, I have to give the check box elements a different
> "name" because they can submit muliple answers for these types of
> questions. I have the following script to check to see if at least one
> element (answer) is checked for each question. I also have hidden elements
> for my ASP page.
>
> I am currently getting "Please check one of boxes in the next question"
> because the checkbox names are different (and need to be).
>
> Here's the HTML:
>
> <HTML>
> <HEAD>
> <link rel="stylesheet" type="text/css" href="EHS.css">
> <script language="JAVASCRIPT">
> function GoForm2(form)
> {
> if (document.Form2.finished[1].checked)
> {
> for (var e = 0; e < form.elements.length; e++)
> {
> var el = form.elements[e];
> if (el.type == 'radio' && el.type != 'hidden')
> {
> var group = form[el.name];
> var checked = false;
> if (!group.length)
> checked = el.checked;
> else
> for (var r = 0; r < group.length; r++)
> if ((checked = group[r].checked))
> break;
> if (!checked)
> {
> alert('Please check one of the radio buttons on the
> next question.');
> el.focus();
> return false;
> }
> }
> else if (el.type == 'checkbox' && el.type != 'hidden')
> {
> var group = form[el.name];
> if (!group.length)
> checked = el.checked;
> else
> for (var r = 0; r < group.length; r++)
> if ((checked = group[r].checked))
> break;
> if (!checked)
> {
> alert('Please check one of the checkboxes on
> the next question.');
> el.focus();
> return false;
> }
> }
> else
> {
> if (el.value == "" || el.value == null || el.value == "
> " && el.type != 'hidden')
> {
> alert('Please answer the next question.');
> el.focus();
> return false;
> }
> }
> }
> return true;
> }
> return true;
> }
> </script>
> </HEAD>
> <body bgcolor="#F0F0DF" leftmargin="0" topmargin="10">
> <form action="Survey-Script.asp" name="Form2" id="Form2" method="post"
> onsubmit="return GoForm2(this)">
> <BR>
> <TABLE align=center border="0" cellPadding="6" bgcolor=#E6E5DC cellSpacing
> ="1" width="70%">
> <TR bgColor=#E6E5DC bordercolor=#E6E5DC><TD colspan="2"></TD></TR>
> <TR bgColor=#E6E5DC>
> <TD align="right">1. </TD>
> <TD>Check all that you are responsible for below.</TD>
> </TR>
> <input type="hidden" Name="QNumber1" value="1">
> <input type="hidden" Name="Q1ACounter" value="4">
> <input type="hidden" Name="QType1" value="1">
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q1A1" value="Erogonomics
> Training" CHECKED> Erogonomics Training
> <input type="hidden" Name="Q1A1B" value="Erogonomics
> Training"></TD>
> </TR>
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q1A2" value="Health Issues"
> CHECKED> Health Issues
> <input type="hidden" Name="Q1A2B" value="Health
> Issues"></TD>
> </TR>
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q1A3" value="Plant Safety">
> Plant Safety<input type="hidden" Name="Q1A3B"></TD>
> </TR>
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q1A4" value="Wastwater
> Protocols"> Wastwater Protocols
> <input type="hidden" Name="Q1A4B"></TD>
> </TR>
> <TR bgColor=#E6E5DC bordercolor=#E6E5DC><TD colspan="2"></TD></TR>
> <TR bgColor=#E6E5DC><TD align="right">2. </TD>
> <TD>Q2 is second?</TD>
> </TR>
> <input type="hidden" Name="QNumber2" value="2">
> <input type="hidden" Name="Q2ACounter" value="3">
> <input type="hidden" Name="QType2" value="1">
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q2A1" value="1"> 1
> <input type="hidden" Name="Q2A1B"></TD>
> </TR>
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q2A2" value="2" CHECKED> 2
> <input type="hidden" Name="Q2A2B" value="2"></TD>
> </TR>
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q2A3" value="3"> 3
> <input type="hidden" Name="Q2A3B"></TD>
> </TR>
> <TR bgColor=#E6E5DC bordercolor=#E6E5DC><TD colspan="2"></TD>
> </TR>
> <TR bgColor=#E6E5DC><TD align="right">3. </TD>
> <TD>Question three OK?</TD>
> </TR>
> <input type="hidden" Name="QNumber3" value="3">
> <input type="hidden" Name="Q3ACounter" value="2">
> <input type="hidden" Name="QType3" value="1">
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q3A1" value="1"> 1
> <input type="hidden" Name="Q3A1B"></TD>
> </TR>
> <TR bgColor=#E6E5DC><TD> </TD>
> <TD><input type="checkbox" Name="Q3A2" value="2"> 2
> <input type="hidden" Name="Q3A2B"></TD>
> </TR>
> <TR bgColor=#E6E5DC bordercolor=#E6E5DC><TD colspan="2"></TD>
> </TR>
> <TR bgcolor="C0C0C0">
> <input type="hidden" name="Survey" value="266">
> <input type="hidden" name="LID" value="322111">
> <input type="hidden" name="Count" value="3">
> <input type="hidden" name="R" value="4">
> <TD align="right"><BR>
> <INPUT type="radio" name="finished" value="N"> Save as
> Incomplete
> </TD>
> <TD align="left"><BR>
> <INPUT type="radio" name="finished" value="Y" checked>
> Submit Final Survey
> </TD>
> </TR>
> <TR bgcolor="C0C0C0">
> <TD colspan="2" align="center">
> <input TYPE="submit" VALUE="Save/Submit Survey" Name
> ="button1"><BR>
> </TD>
> </TR>
> </TABLE>
> </form>
> </body>
> </HTML>
>
> It is still giving me an alert. How do I get around this?
>
--
Shop at ssistant Add-ons and Developer Workshops
http://www.aflyingstart.net/addons/
Enquiries regarding Shop at ssistant Classic training :
Call 01256 880770
Rodney Myers
Based in Oxford, England
Technical Director, Shop at ssistant eCommerce Solutions
More information about the Javascript
mailing list