Checkbox Validation on Dynamic ASP Checkboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • clinttoris@hotmail.com

    Checkbox Validation on Dynamic ASP Checkboxes

    Hello Experts,

    I have been told to post this in the Javascript forum as I want to do
    this client side just before my form gets submitted. Once the user
    clicks the submit button a javascript function needs to run and
    validate all the checkboxes on my form and make sure none of them are
    unchecked. I suck at Javascript and my problem is 2fold. I have the
    following code that constructs the checkbox
    Code:
    response.write "<input type=checkbox Name=""Question" &
    objRS("Question_ID") & """ Value=""" & objTxt("Sub_Text") & """>" &
    objTxt("Text") & "<br>" & chr(13)
    Better yet here is my source code just in case you need it
    [code]
    <form action="testSub mission.asp?Sur vey=" method="post"
    name="SurveySub mitted">

    <input type="hidden" name="HiddenSur veyID" value="1">
    <input type="hidden" name="HiddenQue stionID" value="1">
    <b>1. Which of the following RIM Communication vehicles do you read
    regularly? Please mark all that apply.</b><p>
    <input type=checkbox Name="Question1 " Value="a">Dispa tch Newsletter<br>
    <input type=checkbox Name="Question1 " Value="b">Gener al
    Notifications(e mail)<br>
    <input type=checkbox Name="Question1 " Value="c">Intra net Homepage
    (InSite)<br>
    <input type=checkbox Name="Question1 " Value="d">IT Service Desk
    Corporate Notifications<b r>
    <input type=checkbox Name="Question1 " Value="e">Team Websites
    (http://go/it, http://go/cso etc.)<br>
    <BR>Other<BR><t extarea name="textBoxAn swer1" ></textarea>
    <input type="hidden" name="HiddenTex tValue" value="f">
    <p><b>2. Please select the 3 most helpful means of
    communication?</b><p>
    <input type=checkbox Name="Question2 " Value="a">Onlin e Newsletter<br>
    <input type=checkbox Name="Question2 " Value="b">Print Newsletter<br>
    <input type=checkbox Name="Question2 " Value="c">Intra net homepage
    (Insite)<br>
    <p><b>3. Please select the three least helpful means of
    communication</b><p>
    <p><b>5. To ensure you stay well informed about IT services, projects
    and updates, what communication vehicle would you read? Please select
    one</b><p>
    <p>
    <hr>
    </TABLE>
    <TABLE>
    <TR>
    <TD COLSPAN=3 ALIGN="center"> <BR>
    <input type=submit value="Submit" ONCLICK="javasc ript function;">
    </TD>
    </TR>
    </TABLE>
    [code]

    With this code I need to create a javascript function and all the code
    that I found refers to the check box name in the code. I however, have
    no idea how to refer to my checkbox name because it is created from
    database columns as shown in the above code right before the HTML
    source code. Your help is greatly appreciated.

    Here is the code I found but please if you have something better then
    this please help. Thanks again

    Code:
    function validateCheckBoxes(){
    var oForm = document.SurveySubmitted;//change to real name
    var oCheck = oForm.HiddenTextValue;//change to real name
    var flag = false;
    var i;
    if(!oCheck.length&&oCheck.checked)flag = true;
    else{for(i=0;i<oCheck.length;i++)if(oCheck[i].checked)flag=true;}
    if(!flag){alert("you haven't checked anything");return false;}
    else oForm.submit();
    }
  • Ben Amada

    #2
    Re: Checkbox Validation on Dynamic ASP Checkboxes

    clinttoris@hotm ail.com wrote:
    [color=blue]
    > I have the
    > following code that constructs the checkbox
    >
    Code:
    > response.write "<input type=checkbox Name=""Question" &
    > objRS("Question_ID") & """ Value=""" & objTxt("Sub_Text") & """>" &
    > objTxt("Text") & "<br>" & chr(13)
    >
    > With this code I need to create a javascript function and all the code
    > that I found refers to the check box name in the code. I however, have
    > no idea how to refer to my checkbox name because it is created from
    > database columns as shown in the above code right before the HTML
    > source code. Your help is greatly appreciated.[/color]

    Hi. Here's a pretty decent approach:

    1. Assign each checkbox a unique ID using sequential numbers (chk1, chk2 ...
    chk7). In your ASP code, you can do this by creating an integer variable
    you increment by 1 each time you output a checkbox control.

    2. Store the largest sequential number in a hidden field (for example, 7).

    3. In validation routine, use a "for" loop -- looping from 1 thru 7 --
    checking the status of each checkbox.

    Ben


    Comment

    • clinttoris@hotmail.com

      #3
      Re: Checkbox Validation on Dynamic ASP Checkboxes


      Ben Amada wrote:[color=blue]
      > clinttoris@hotm ail.com wrote:
      >[color=green]
      > > I have the
      > > following code that constructs the checkbox
      > >
      Code:
      > > response.write "<input type=checkbox Name=""Question" &
      > > objRS("Question_ID") & """ Value=""" & objTxt("Sub_Text") & """>" &
      > > objTxt("Text") & "<br>" & chr(13)
      > >
      > > With this code I need to create a javascript function and all the code
      > > that I found refers to the check box name in the code. I however, have
      > > no idea how to refer to my checkbox name because it is created from
      > > database columns as shown in the above code right before the HTML
      > > source code. Your help is greatly appreciated.[/color]
      >
      > Hi. Here's a pretty decent approach:
      >
      > 1. Assign each checkbox a unique ID using sequential numbers (chk1, chk2 ...
      > chk7). In your ASP code, you can do this by creating an integer variable
      > you increment by 1 each time you output a checkbox control.
      >
      > 2. Store the largest sequential number in a hidden field (for example, 7).
      >
      > 3. In validation routine, use a "for" loop -- looping from 1 thru 7 --
      > checking the status of each checkbox.
      >
      > Ben[/color]

      Hi Ben,

      I'm going to have to say I don't understand. I do not wish to change
      the structure since everything is working fine up until this point.
      Could you clarify your point with some code. Sorry.

      Comment

      • Ben Amada

        #4
        Re: Checkbox Validation on Dynamic ASP Checkboxes

        clinttoris@hotm ail.com wrote:
        [color=blue]
        > Ben Amada wrote:[color=green]
        >>
        >> Hi. Here's a pretty decent approach:
        >>
        >> 1. Assign each checkbox a unique ID using sequential numbers (chk1, chk2
        >> ... chk7). In your ASP code, you can do this by creating an integer
        >> variable you increment by 1 each time you output a checkbox control.
        >>
        >> 2. Store the largest sequential number in a hidden field (for example,
        >> 7).
        >>
        >> 3. In validation routine, use a "for" loop -- looping from 1 thru 7 --
        >> checking the status of each checkbox.
        >>
        >> Ben[/color]
        >
        > Hi Ben,
        >
        > I'm going to have to say I don't understand. I do not wish to change
        > the structure since everything is working fine up until this point.
        > Could you clarify your point with some code. Sorry.[/color]

        The code below is untested, but should get you close to what you're looking
        for with few changes to what you've already got:

        === ASP code ===

        Dim iCheckboxID As Integer
        iCheckboxID = 0

        While Not objRS.EOF

        iCheckboxID = iCheckboxID + 1

        response.write "<input type=""checkbox "" id=""" & _
        "chk" & iCheckboxID & """ Name=""Question " & _
        objRS("Question _ID") & """ Value=""" & _
        objTxt("Sub_Tex t") & """>" & _
        objTxt("Text") & "<br>" & chr(13)

        objRS.MoveNext

        End While

        response.write "<input type=""hidden"" " & _
        "id=""CheckboxC ount"" " & _
        "value=""" & iCheckboxID & """>"

        === JavaScript code ===

        function validateCheckBo xes(){
        var bFlag = false;
        var iCheckboxCount = document.getEle mentById('Check boxCount').valu e;
        for (var i=1; i <= iCheckboxCount; i++) {
        if (document.getEl ementById("chk" + i).checked) {
        bFlag = true;
        }
        }
        if (!bFlag) {
        alert("you haven't checked anything");
        return false;
        } else {
        var oForm = document.Survey Submitted;//change to real name
        oForm.submit();
        }
        }


        Comment

        • clinttoris@hotmail.com

          #5
          Re: Checkbox Validation on Dynamic ASP Checkboxes

          Thanks for accomodating Ben,

          I have implemented the code without errors but it is not doing
          anything. Here is my HTM source code and it looks fine.

          <form action="testSub mission.asp?Sur vey=" method="post"
          name="SurveySub mitted">

          <input type="hidden" name="HiddenSur veyID" value="1">
          <input type="hidden" name="HiddenQue stionID" value="1">
          <b>1. Which of the following RIM Communication vehicles do you read
          regularly? Please mark all that apply.</b><p>
          <input type="checkbox" id="chk1" Name="Question1 " Value="a">Dispa tch
          Newsletter<br>
          <input type="checkbox" id="chk1" Name="Question1 " Value="b">Gener al
          Notifications(e mail)<br>
          <input type="checkbox" id="chk1" Name="Question1 " Value="c">Intra net
          Homepage (InSite)<br>
          <input type="checkbox" id="chk1" Name="Question1 " Value="d">IT Service
          Desk Corporate Notifications<b r>
          <input type="checkbox" id="chk1" Name="Question1 " Value="e">Team
          Websites (http://go/it, http://go/cso etc.)<br>
          <BR>Other<BR><t extarea name="textBoxAn swer1" rows="2"
          cols="50"></textarea>
          <input type="hidden" name="HiddenTex tValue" value="f">
          <input type="hidden" id="CheckboxCou nt" value="1"><p><b >2. Please
          select the 3 most helpful means of communication?</b><p>
          <input type="checkbox" id="chk2" Name="Question2 " Value="a">Onlin e
          Newsletter<br>
          <input type="checkbox" id="chk2" Name="Question2 " Value="b">Print
          Newsletter<br>
          <input type="checkbox" id="chk2" Name="Question2 " Value="c">Intra net
          homepage (Insite)<br>
          <input type="hidden" id="CheckboxCou nt" value="2"><p><b >3. Please
          select the three least helpful means of communication</b><p>
          <input type="hidden" id="CheckboxCou nt" value="2"><p><b >5. To ensure
          you stay well informed about IT services, projects and updates, what
          communication vehicle would you read? Please select one</b><p>
          <input type="hidden" id="CheckboxCou nt" value="2"><p>
          <hr>
          </TABLE>
          <TABLE>
          <TR>
          <TD COLSPAN=3 ALIGN="center"> <BR>
          <input type=submit value="Submit" ONCLICK="valida teCheckBoxes(); ">

          </TD>
          </TR>
          </TABLE>
          </FORM>
          </CENTER>




          Ben Amada wrote:[color=blue]
          > clinttoris@hotm ail.com wrote:
          >[color=green]
          > > Ben Amada wrote:[color=darkred]
          > >>
          > >> Hi. Here's a pretty decent approach:
          > >>
          > >> 1. Assign each checkbox a unique ID using sequential numbers (chk1, chk2
          > >> ... chk7). In your ASP code, you can do this by creating an integer
          > >> variable you increment by 1 each time you output a checkbox control.
          > >>
          > >> 2. Store the largest sequential number in a hidden field (for example,
          > >> 7).
          > >>
          > >> 3. In validation routine, use a "for" loop -- looping from 1 thru 7 --
          > >> checking the status of each checkbox.
          > >>
          > >> Ben[/color]
          > >
          > > Hi Ben,
          > >
          > > I'm going to have to say I don't understand. I do not wish to change
          > > the structure since everything is working fine up until this point.
          > > Could you clarify your point with some code. Sorry.[/color]
          >
          > The code below is untested, but should get you close to what you're looking
          > for with few changes to what you've already got:
          >
          > === ASP code ===
          >
          > Dim iCheckboxID As Integer
          > iCheckboxID = 0
          >
          > While Not objRS.EOF
          >
          > iCheckboxID = iCheckboxID + 1
          >
          > response.write "<input type=""checkbox "" id=""" & _
          > "chk" & iCheckboxID & """ Name=""Question " & _
          > objRS("Question _ID") & """ Value=""" & _
          > objTxt("Sub_Tex t") & """>" & _
          > objTxt("Text") & "<br>" & chr(13)
          >
          > objRS.MoveNext
          >
          > End While
          >
          > response.write "<input type=""hidden"" " & _
          > "id=""CheckboxC ount"" " & _
          > "value=""" & iCheckboxID & """>"
          >
          > === JavaScript code ===
          >
          > function validateCheckBo xes(){
          > var bFlag = false;
          > var iCheckboxCount = document.getEle mentById('Check boxCount').valu e;
          > for (var i=1; i <= iCheckboxCount; i++) {
          > if (document.getEl ementById("chk" + i).checked) {
          > bFlag = true;
          > }
          > }
          > if (!bFlag) {
          > alert("you haven't checked anything");
          > return false;
          > } else {
          > var oForm = document.Survey Submitted;//change to real name
          > oForm.submit();
          > }
          > }[/color]

          Comment

          • clinttoris@hotmail.com

            #6
            Re: Checkbox Validation on Dynamic ASP Checkboxes

            Thanks for accomodating Ben,

            I have implemented the code without errors but it is not doing
            anything. Here is my HTM source code and it looks fine.

            <form action="testSub mission.asp?Sur vey=" method="post"
            name="SurveySub mitted">

            <input type="hidden" name="HiddenSur veyID" value="1">
            <input type="hidden" name="HiddenQue stionID" value="1">
            <b>1. Which of the following RIM Communication vehicles do you read
            regularly? Please mark all that apply.</b><p>
            <input type="checkbox" id="chk1" Name="Question1 " Value="a">Dispa tch
            Newsletter<br>
            <input type="checkbox" id="chk1" Name="Question1 " Value="b">Gener al
            Notifications(e mail)<br>
            <input type="checkbox" id="chk1" Name="Question1 " Value="c">Intra net
            Homepage (InSite)<br>
            <input type="checkbox" id="chk1" Name="Question1 " Value="d">IT Service
            Desk Corporate Notifications<b r>
            <input type="checkbox" id="chk1" Name="Question1 " Value="e">Team
            Websites (http://go/it, http://go/cso etc.)<br>
            <BR>Other<BR><t extarea name="textBoxAn swer1" rows="2"
            cols="50"></textarea>
            <input type="hidden" name="HiddenTex tValue" value="f">
            <input type="hidden" id="CheckboxCou nt" value="1"><p><b >2. Please
            select the 3 most helpful means of communication?</b><p>
            <input type="checkbox" id="chk2" Name="Question2 " Value="a">Onlin e
            Newsletter<br>
            <input type="checkbox" id="chk2" Name="Question2 " Value="b">Print
            Newsletter<br>
            <input type="checkbox" id="chk2" Name="Question2 " Value="c">Intra net
            homepage (Insite)<br>
            <input type="hidden" id="CheckboxCou nt" value="2"><p><b >3. Please
            select the three least helpful means of communication</b><p>
            <input type="hidden" id="CheckboxCou nt" value="2"><p><b >5. To ensure
            you stay well informed about IT services, projects and updates, what
            communication vehicle would you read? Please select one</b><p>
            <input type="hidden" id="CheckboxCou nt" value="2"><p>
            <hr>
            </TABLE>
            <TABLE>
            <TR>
            <TD COLSPAN=3 ALIGN="center"> <BR>
            <input type=submit value="Submit" ONCLICK="valida teCheckBoxes(); ">

            </TD>
            </TR>
            </TABLE>
            </FORM>
            </CENTER>




            Ben Amada wrote:[color=blue]
            > clinttoris@hotm ail.com wrote:
            >[color=green]
            > > Ben Amada wrote:[color=darkred]
            > >>
            > >> Hi. Here's a pretty decent approach:
            > >>
            > >> 1. Assign each checkbox a unique ID using sequential numbers (chk1, chk2
            > >> ... chk7). In your ASP code, you can do this by creating an integer
            > >> variable you increment by 1 each time you output a checkbox control.
            > >>
            > >> 2. Store the largest sequential number in a hidden field (for example,
            > >> 7).
            > >>
            > >> 3. In validation routine, use a "for" loop -- looping from 1 thru 7 --
            > >> checking the status of each checkbox.
            > >>
            > >> Ben[/color]
            > >
            > > Hi Ben,
            > >
            > > I'm going to have to say I don't understand. I do not wish to change
            > > the structure since everything is working fine up until this point.
            > > Could you clarify your point with some code. Sorry.[/color]
            >
            > The code below is untested, but should get you close to what you're looking
            > for with few changes to what you've already got:
            >
            > === ASP code ===
            >
            > Dim iCheckboxID As Integer
            > iCheckboxID = 0
            >
            > While Not objRS.EOF
            >
            > iCheckboxID = iCheckboxID + 1
            >
            > response.write "<input type=""checkbox "" id=""" & _
            > "chk" & iCheckboxID & """ Name=""Question " & _
            > objRS("Question _ID") & """ Value=""" & _
            > objTxt("Sub_Tex t") & """>" & _
            > objTxt("Text") & "<br>" & chr(13)
            >
            > objRS.MoveNext
            >
            > End While
            >
            > response.write "<input type=""hidden"" " & _
            > "id=""CheckboxC ount"" " & _
            > "value=""" & iCheckboxID & """>"
            >
            > === JavaScript code ===
            >
            > function validateCheckBo xes(){
            > var bFlag = false;
            > var iCheckboxCount = document.getEle mentById('Check boxCount').valu e;
            > for (var i=1; i <= iCheckboxCount; i++) {
            > if (document.getEl ementById("chk" + i).checked) {
            > bFlag = true;
            > }
            > }
            > if (!bFlag) {
            > alert("you haven't checked anything");
            > return false;
            > } else {
            > var oForm = document.Survey Submitted;//change to real name
            > oForm.submit();
            > }
            > }[/color]

            Comment

            • Ben Amada

              #7
              Re: Checkbox Validation on Dynamic ASP Checkboxes

              clinttoris@hotm ail.com wrote:
              [color=blue]
              > Thanks for accomodating Ben,
              >
              > I have implemented the code without errors but it is not doing
              > anything. Here is my HTM source code and it looks fine.[/color]

              Thanks for posting the outputted HTML. I now see a couple of problems ...

              (1) Each checkbox should have a unique "id".

              (2) However, there is another problem in that I initially didn't realize you
              have different sets of questions where (I'm guessing) at least one box must
              be checked for each set of questions.

              (3) Each checkbox 'input' tag should have a closing tag -- </input>.

              --- RECOMMENDATIONS ---

              (1) Use the following checkbox "id" scheme:

              - For question 1's checkbox IDs, use "chk1_1", "chk1_2", "chk1_3", etc.
              - For question 2's checkbox IDs, use "chk2_1", "chk2_2", "chk2_3", etc.

              (2) Create one hidden input field for each set of questions:

              - For question 1, create hidden input field "cbCount1" with a value
              containing the number of checkboxes under question 1.
              - For question 2, create hidden input field "cbCount2" with a value
              containing the number of checkboxes under question 2.
              etc.

              (3) Create one hidden input field (QuestionCount) which contains the number
              of questions you have on the page.

              ---------
              If you follow the above 3 recommendations , then the JavaScript below should
              work to make sure at least one box has been checked for each set of
              questions.

              === JavaScript code Below ===

              function validateCheckBo xes(){

              var bOK = true;
              var bFlag = false;
              var iCheckboxCount = 0;
              var iQuestionCount = document.getEle mentById('Quest ionCount').valu e;

              for (var i=1; i <= iQuestionCount; i++) {
              bFlag = false;
              iCheckboxCount = document.getEle mentById('cbCou nt' + i).value;

              for (var j=1; j <= iCheckboxCount; j++) {
              if (document.getEl ementById("chk" + i + "_" + j).checked) {
              bFlag = true;
              }
              }

              if (!bFlag) { bOK = false; }
              }

              if (!bOK) {
              alert("not all questions answered");
              return false;
              } else {
              var oForm = document.Survey Submitted;//change to real name
              oForm.submit();
              }

              }


              Comment

              • clinttoris@hotmail.com

                #8
                Re: Checkbox Validation on Dynamic ASP Checkboxes

                O.k Ben you know what I'm gonna ask you next right?
                Well I'm not very good working with Dynamic data. Could you provide
                some coe for the recommendation. Obviously that is not right of me to
                ask but when I see something I understand it more thoroughly for the
                next time. I'm not gonna lie I'm not sure how to complete what you
                asked. Sure I understand the hidden field part but the rest not sure.
                Anyway you can provide. Thanks again Ben.

                And yes, each question should have at least one checkbox checked.


                Ben Amada wrote:[color=blue]
                > clinttoris@hotm ail.com wrote:
                >[color=green]
                > > Thanks for accomodating Ben,
                > >
                > > I have implemented the code without errors but it is not doing
                > > anything. Here is my HTM source code and it looks fine.[/color]
                >
                > Thanks for posting the outputted HTML. I now see a couple of problems ...
                >
                > (1) Each checkbox should have a unique "id".
                >
                > (2) However, there is another problem in that I initially didn't realize you
                > have different sets of questions where (I'm guessing) at least one box must
                > be checked for each set of questions.
                >
                > (3) Each checkbox 'input' tag should have a closing tag -- </input>.
                >
                > --- RECOMMENDATIONS ---
                >
                > (1) Use the following checkbox "id" scheme:
                >
                > - For question 1's checkbox IDs, use "chk1_1", "chk1_2", "chk1_3", etc.
                > - For question 2's checkbox IDs, use "chk2_1", "chk2_2", "chk2_3", etc.
                >
                > (2) Create one hidden input field for each set of questions:
                >
                > - For question 1, create hidden input field "cbCount1" with a value
                > containing the number of checkboxes under question 1.
                > - For question 2, create hidden input field "cbCount2" with a value
                > containing the number of checkboxes under question 2.
                > etc.
                >
                > (3) Create one hidden input field (QuestionCount) which contains the number
                > of questions you have on the page.
                >
                > ---------
                > If you follow the above 3 recommendations , then the JavaScript below should
                > work to make sure at least one box has been checked for each set of
                > questions.
                >
                > === JavaScript code Below ===
                >
                > function validateCheckBo xes(){
                >
                > var bOK = true;
                > var bFlag = false;
                > var iCheckboxCount = 0;
                > var iQuestionCount = document.getEle mentById('Quest ionCount').valu e;
                >
                > for (var i=1; i <= iQuestionCount; i++) {
                > bFlag = false;
                > iCheckboxCount = document.getEle mentById('cbCou nt' + i).value;
                >
                > for (var j=1; j <= iCheckboxCount; j++) {
                > if (document.getEl ementById("chk" + i + "_" + j).checked) {
                > bFlag = true;
                > }
                > }
                >
                > if (!bFlag) { bOK = false; }
                > }
                >
                > if (!bOK) {
                > alert("not all questions answered");
                > return false;
                > } else {
                > var oForm = document.Survey Submitted;//change to real name
                > oForm.submit();
                > }
                >
                > }[/color]

                Comment

                • Ben Amada

                  #9
                  Re: Checkbox Validation on Dynamic ASP Checkboxes

                  clinttoris@hotm ail.com wrote:
                  [color=blue]
                  > Well I'm not very good working with Dynamic data. Could you provide
                  > some coe for the recommendation. Obviously that is not right of me to
                  > ask but when I see something I understand it more thoroughly for the
                  > next time. I'm not gonna lie I'm not sure how to complete what you
                  > asked. Sure I understand the hidden field part but the rest not sure.
                  > Anyway you can provide. Thanks again Ben.
                  >
                  > And yes, each question should have at least one checkbox checked.[/color]

                  I believe you're asking about the ASP coding. Basically you'll want to
                  maintain 2 variables, iQuestionID and iCheckboxID, where iQuestionID keeps
                  track of the current question id and iCheckboxID keeps track of the current
                  checkbox id WITHIN (or FOR) the current question. Not being familiar with
                  the details of your code, the sample code below is just an example of what
                  you are probably looking for.

                  Dim iQuestionID As Integer
                  Dim iCheckboxID As Integer
                  Dim strCurrentQuest ionID As String

                  While Not objRS.EOF

                  If objRS("Question _ID") <> strCurrentQuest ionID Then

                  If iQuestionID > 0 Then

                  response.write "<input type=""hidden"" " & _
                  "id=""cbCount"" " & iQuestionID & " " & _
                  "value=""" & iCheckboxID & """>"

                  End If

                  strCurrentQuest ionID = objRS("Question _ID")
                  iQuestionID = iQuestionID + 1
                  iCheckboxID = 0

                  response.write "Question #" & iQuestionID & "<br>"

                  End If

                  iCheckboxID = iCheckboxID + 1

                  response.write "<input type=""checkbox "" id=""" & _
                  "chk" & iQuestionID & "_" & iCheckboxID & """ " & _
                  "Name=""Questio n" & _
                  objRS("Question _ID") & """ Value=""" & _
                  objTxt("Sub_Tex t") & """>" & _
                  objTxt("Text") & "</input><br>" & chr(13)

                  objRS.MoveNext
                  End While

                  response.write "<input type=""hidden"" " & _
                  "id=""QuestionC ount"" " & _
                  "value=""" & iQuestionID & """>"


                  Comment

                  • Ben Amada

                    #10
                    Re: Checkbox Validation on Dynamic ASP Checkboxes

                    Ben Amada wrote:
                    [color=blue]
                    > If iQuestionID > 0 Then
                    >
                    > response.write "<input type=""hidden"" " & _
                    > "id=""cbCount"" " & iQuestionID & " " & _
                    > "value=""" & iCheckboxID & """>"
                    >
                    > End If[/color]

                    .... oops (minor mistake), the above code should read:

                    If iQuestionID > 0 Then

                    response.write "<input type=""hidden"" " & _
                    "id=""cbCou nt" & iQuestionID & """ " & _
                    "value=""" & iCheckboxID & """>"

                    End If

                    ----------
                    Good luck!
                    Ben


                    Comment

                    • Ben Amada

                      #11
                      Re: Checkbox Validation on Dynamic ASP Checkboxes

                      .... one more glitch (obviously I've just been typing air-code :) You'll
                      want to output one last hidden input field after the While loop is over.

                      ....
                      ....
                      End While

                      If iQuestionID > 0 Then
                      response.write "<input type=""hidden"" " & _
                      "id=""cbCou nt" & iQuestionID & """ " & _
                      "value=""" & iCheckboxID & """>"
                      End If

                      response.write "<input type=""hidden"" " & _
                      "id=""QuestionC ount"" " & _
                      "value=""" & iQuestionID & """>"
                      If iQuestionID > 0 Then


                      Comment

                      • clinttoris@hotmail.com

                        #12
                        Re: Checkbox Validation on Dynamic ASP Checkboxes

                        Wow Ben,

                        I would had to start changing code that I worked so hard on. What if I
                        wanted to implement your code to my code. This is my code. Short and
                        sweet. Could it not be incorporated into my code

                        <BODY leftmargin="0" rightmargin="0" marginwidth="0" topmargin="0"
                        marginheight="0 " bottommargin="0 " bgcolor="#fffff f">
                        <table width="60%" cellspacing="0" cellpadding="5" >
                        <%
                        Set oConn = Server.CreateOb ject("ADODB.Con nection")
                        oConn.Open "DSN"
                        'now find the quiz specified by the URL command line
                        'e.g. vbquiz.asp?quiz =1 means show quiz 1
                        survey=1
                        set objRS = oConn.Execute(" SELECT * FROM Survey WHERE Survey_ID=" &
                        survey)
                        %>
                        <hr>
                        <font size=5>
                        <center><b><% =objRS("Descr") %></center></b>
                        <HR>
                        </font>
                        <p>
                        <form
                        action="testSub mission.asp?Sur vey=<%=request. querystring("Su rvey") %>"
                        method="post" name="SurveySub mitted">
                        <%
                        objRS.close
                        'now start the main loop to find all the questions

                        set objRS = oConn.Execute(" SELECT * FROM survey_question s WHERE
                        Survey_ID=" & survey & " ORDER BY Question_ID")

                        If not objRS.eof Then
                        %>
                        <input type="hidden" name="HiddenSur veyID"
                        value="<%=objRS ("Survey_ID")%> ">
                        <input type="hidden" name="HiddenQue stionID"
                        value="<%=objRS ("Question_ID") %>">
                        <%
                        End IF
                        if not objRS.EOF then
                        objRS.movefirst
                        do
                        response.write "<b>" & objRS("Question _ID") & ". " &
                        objRS("Question ") & "</b><p>" & chr(13)
                        'now display the available options
                        set objTxt = oConn.Execute(" SELECT * FROM survey_user_tex t WHERE
                        Question_ID=" & objRS("Question _ID") )
                        if not objTxt.EOF then
                        objTxt.movefirs t
                        Do
                        If (objTxt("Type") ) = "radio" then
                        response.write "<input type=checkbox Name=""Question " &
                        objRS("Question _ID") & """ Value=""" & objTxt("Sub_Tex t") & """>" &
                        objTxt("Text") & "<br>" & chr(13)
                        ELSE
                        response.write "<BR>" & objTxt("Text")& "<BR>" %><textarea
                        name="textBoxAn swer<%=objRS("Q uestion_Id")%>" rows="2"
                        cols="50"></textarea>
                        <input type="hidden" name="HiddenTex tValue"
                        value="<%=objTx t("sub_text")%> ">
                        <%
                        End IF
                        objTxt.movenext
                        loop until objTxt.EOF
                        end if
                        response.write "<p>"
                        objRS.movenext
                        loop until objRS.EOF
                        end if
                        oConn.close
                        %>
                        <hr>
                        </TABLE>
                        <TABLE>
                        <TR>
                        <TD COLSPAN=3 ALIGN="center"> <BR>
                        <input type=submit value="Submit" ONCLICK="valida teCheckBoxes(); ">
                        </TD>
                        </TR>
                        </TABLE>
                        </FORM>
                        </CENTER>


                        Ben Amada wrote:[color=blue]
                        > ... one more glitch (obviously I've just been typing air-code :) You'll
                        > want to output one last hidden input field after the While loop is over.
                        >
                        > ...
                        > ...
                        > End While
                        >
                        > If iQuestionID > 0 Then
                        > response.write "<input type=""hidden"" " & _
                        > "id=""cbCou nt" & iQuestionID & """ " & _
                        > "value=""" & iCheckboxID & """>"
                        > End If
                        >
                        > response.write "<input type=""hidden"" " & _
                        > "id=""QuestionC ount"" " & _
                        > "value=""" & iQuestionID & """>"
                        > If iQuestionID > 0 Then[/color]

                        Comment

                        • Ben Amada

                          #13
                          Re: Checkbox Validation on Dynamic ASP Checkboxes

                          clinttoris@hotm ail.com wrote:
                          [color=blue]
                          > Wow Ben,
                          >
                          > I would had to start changing code that I worked so hard on. What if I
                          > wanted to implement your code to my code. This is my code. Short and
                          > sweet. Could it not be incorporated into my code[/color]

                          Didn't mean to overwhelm you :) I was able to basically copy the ASP
                          code from my other post and paste it into your ASP code (see below).
                          It is untested as I don't have a database setup -- but should work.
                          The only other thing you'd want to do is insert the JavaScript I posted
                          a few posts ago into your code.

                          Good luck,
                          Ben
                          ---------------------------------------------

                          <BODY leftmargin="0" rightmargin="0" marginwidth="0" topmargin="0"
                          marginheight="0 " bottommargin="0 " bgcolor="#fffff f">
                          <table width="60%" cellspacing="0" cellpadding="5" >
                          <%
                          Dim iQuestionID As Integer
                          Dim iCheckboxID As Integer
                          Dim strCurrentQuest ionID As String
                          Set oConn = Server.CreateOb ject("ADODB.Con nection")
                          oConn.Open "DSN"
                          'now find the quiz specified by the URL command line
                          'e.g. vbquiz.asp?quiz =1 means show quiz 1
                          survey=1
                          set objRS = oConn.Execute(" SELECT * FROM Survey WHERE Survey_ID=" &
                          survey)
                          %>
                          <hr>
                          <font size=5>
                          <center><b><% =objRS("Descr") %></center></b>
                          <HR>
                          </font>
                          <p>
                          <form
                          action="testSub mission.asp?Sur vey=<%=request. querystring("Su rvey") %>"
                          method="post" name="SurveySub mitted">
                          <%
                          objRS.close
                          'now start the main loop to find all the questions

                          set objRS = oConn.Execute(" SELECT * FROM survey_question s WHERE
                          Survey_ID=" & survey & " ORDER BY Question_ID")

                          If not objRS.eof Then
                          %>
                          <input type="hidden" name="HiddenSur veyID"
                          value="<%=objRS ("Survey_ID")%> ">
                          <input type="hidden" name="HiddenQue stionID"
                          value="<%=objRS ("Question_ID") %>">
                          <%
                          End IF
                          if not objRS.EOF then
                          objRS.movefirst
                          do
                          response.write "<b>" & objRS("Question _ID") & ". " &
                          objRS("Question ") & "</b><p>" & chr(13)
                          'now display the available options
                          set objTxt = oConn.Execute(" SELECT * FROM
                          survey_user_tex t WHERE
                          Question_ID=" & objRS("Question _ID") )
                          if not objTxt.EOF then

                          objTxt.movefirs t
                          Do

                          If (objTxt("Type") ) = "radio" then

                          If objRS("Question _ID") <> strCurrentQuest ionID Then

                          If iQuestionID > 0 Then
                          response.write "<input type=""hidden"" " & _
                          "id=""cbCou nt" & iQuestionID & """ " & _
                          "value=""" & iCheckboxID & """>"
                          End If

                          strCurrentQuest ionID = objRS("Question _ID")
                          iQuestionID = iQuestionID + 1
                          iCheckboxID = 0

                          'response.write "Question #" & iQuestionID & "<br>"

                          End If

                          iCheckboxID = iCheckboxID + 1

                          response.write "<input type=""checkbox "" id=""" & _
                          "chk" & iQuestionID & "_" & iCheckboxID & """ " & _
                          "Name=""Questio n" & _
                          objRS("Question _ID") & """ Value=""" & _
                          objTxt("Sub_Tex t") & """>" & _
                          objTxt("Text") & "</input><br>" & chr(13)

                          ELSE
                          response.write "<BR>" & objTxt("Text")& "<BR>" %><textarea
                          name="textBoxAn swer<%=objRS("Q uestion_Id")%>" rows="2"
                          cols="50"></textarea>
                          <input type="hidden" name="HiddenTex tValue"
                          value="<%=objTx t("sub_text")%> ">
                          <%
                          End IF
                          objTxt.movenext
                          loop until objTxt.EOF

                          end if
                          response.write "<p>"
                          objRS.movenext
                          loop until objRS.EOF

                          If iQuestionID > 0 Then
                          response.write "<input type=""hidden"" " & _
                          "id=""cbCou nt" & iQuestionID & """ " & _
                          "value=""" & iCheckboxID & """>"
                          End If

                          response.write "<input type=""hidden"" " & _
                          "id=""QuestionC ount"" " & _
                          "value=""" & iQuestionID & """>"

                          end if
                          oConn.close
                          %>
                          <hr>
                          </TABLE>
                          <TABLE>
                          <TR>
                          <TD COLSPAN=3 ALIGN="center"> <BR>
                          <input type=submit value="Submit"
                          ONCLICK="valida teCheckBoxes(); ">
                          </TD>
                          </TR>
                          </TABLE>
                          </FORM>
                          </CENTER>

                          Comment

                          • clinttoris@hotmail.com

                            #14
                            Re: Checkbox Validation on Dynamic ASP Checkboxes

                            Hello again Ben and I really appreciate this.
                            I am getting a Type Mismatch error with respect to this line

                            If objRS("Question _ID") <> strCurrentQuest ionID Then

                            Also, I did not declare a type for the following variables since you do
                            not have to in asp and secondly it gives me an error. I think this is
                            the cause of the type mismatch. Any ideas?

                            Dim iQuestionID As Integer
                            Dim iCheckboxID As Integer
                            Dim strCurrentQuest ionID As String




                            Ben Amada wrote:[color=blue]
                            > clinttoris@hotm ail.com wrote:
                            >[color=green]
                            > > Wow Ben,
                            > >
                            > > I would had to start changing code that I worked so hard on. What if I
                            > > wanted to implement your code to my code. This is my code. Short and
                            > > sweet. Could it not be incorporated into my code[/color]
                            >
                            > Didn't mean to overwhelm you :) I was able to basically copy the ASP
                            > code from my other post and paste it into your ASP code (see below).
                            > It is untested as I don't have a database setup -- but should work.
                            > The only other thing you'd want to do is insert the JavaScript I posted
                            > a few posts ago into your code.
                            >
                            > Good luck,
                            > Ben
                            > ---------------------------------------------
                            >
                            > <BODY leftmargin="0" rightmargin="0" marginwidth="0" topmargin="0"
                            > marginheight="0 " bottommargin="0 " bgcolor="#fffff f">
                            > <table width="60%" cellspacing="0" cellpadding="5" >
                            > <%
                            > Dim iQuestionID As Integer
                            > Dim iCheckboxID As Integer
                            > Dim strCurrentQuest ionID As String
                            > Set oConn = Server.CreateOb ject("ADODB.Con nection")
                            > oConn.Open "DSN"
                            > 'now find the quiz specified by the URL command line
                            > 'e.g. vbquiz.asp?quiz =1 means show quiz 1
                            > survey=1
                            > set objRS = oConn.Execute(" SELECT * FROM Survey WHERE Survey_ID=" &
                            > survey)
                            > %>
                            > <hr>
                            > <font size=5>
                            > <center><b><% =objRS("Descr") %></center></b>
                            > <HR>
                            > </font>
                            > <p>
                            > <form
                            > action="testSub mission.asp?Sur vey=<%=request. querystring("Su rvey") %>"
                            > method="post" name="SurveySub mitted">
                            > <%
                            > objRS.close
                            > 'now start the main loop to find all the questions
                            >
                            > set objRS = oConn.Execute(" SELECT * FROM survey_question s WHERE
                            > Survey_ID=" & survey & " ORDER BY Question_ID")
                            >
                            > If not objRS.eof Then
                            > %>
                            > <input type="hidden" name="HiddenSur veyID"
                            > value="<%=objRS ("Survey_ID")%> ">
                            > <input type="hidden" name="HiddenQue stionID"
                            > value="<%=objRS ("Question_ID") %>">
                            > <%
                            > End IF
                            > if not objRS.EOF then
                            > objRS.movefirst
                            > do
                            > response.write "<b>" & objRS("Question _ID") & ". " &
                            > objRS("Question ") & "</b><p>" & chr(13)
                            > 'now display the available options
                            > set objTxt = oConn.Execute(" SELECT * FROM
                            > survey_user_tex t WHERE
                            > Question_ID=" & objRS("Question _ID") )
                            > if not objTxt.EOF then
                            >
                            > objTxt.movefirs t
                            > Do
                            >
                            > If (objTxt("Type") ) = "radio" then
                            >
                            > If objRS("Question _ID") <> strCurrentQuest ionID Then
                            >
                            > If iQuestionID > 0 Then
                            > response.write "<input type=""hidden"" " & _
                            > "id=""cbCou nt" & iQuestionID & """ " & _
                            > "value=""" & iCheckboxID & """>"
                            > End If
                            >
                            > strCurrentQuest ionID = objRS("Question _ID")
                            > iQuestionID = iQuestionID + 1
                            > iCheckboxID = 0
                            >
                            > 'response.write "Question #" & iQuestionID & "<br>"
                            >
                            > End If
                            >
                            > iCheckboxID = iCheckboxID + 1
                            >
                            > response.write "<input type=""checkbox "" id=""" & _
                            > "chk" & iQuestionID & "_" & iCheckboxID & """ " & _
                            > "Name=""Questio n" & _
                            > objRS("Question _ID") & """ Value=""" & _
                            > objTxt("Sub_Tex t") & """>" & _
                            > objTxt("Text") & "</input><br>" & chr(13)
                            >
                            > ELSE
                            > response.write "<BR>" & objTxt("Text")& "<BR>" %><textarea
                            > name="textBoxAn swer<%=objRS("Q uestion_Id")%>" rows="2"
                            > cols="50"></textarea>
                            > <input type="hidden" name="HiddenTex tValue"
                            > value="<%=objTx t("sub_text")%> ">
                            > <%
                            > End IF
                            > objTxt.movenext
                            > loop until objTxt.EOF
                            >
                            > end if
                            > response.write "<p>"
                            > objRS.movenext
                            > loop until objRS.EOF
                            >
                            > If iQuestionID > 0 Then
                            > response.write "<input type=""hidden"" " & _
                            > "id=""cbCou nt" & iQuestionID & """ " & _
                            > "value=""" & iCheckboxID & """>"
                            > End If
                            >
                            > response.write "<input type=""hidden"" " & _
                            > "id=""QuestionC ount"" " & _
                            > "value=""" & iQuestionID & """>"
                            >
                            > end if
                            > oConn.close
                            > %>
                            > <hr>
                            > </TABLE>
                            > <TABLE>
                            > <TR>
                            > <TD COLSPAN=3 ALIGN="center"> <BR>
                            > <input type=submit value="Submit"
                            > ONCLICK="valida teCheckBoxes(); ">
                            > </TD>
                            > </TR>
                            > </TABLE>
                            > </FORM>
                            > </CENTER>[/color]

                            Comment

                            • clinttoris@hotmail.com

                              #15
                              Re: Checkbox Validation on Dynamic ASP Checkboxes

                              Hello again Ben and I really appreciate this.
                              I am getting a Type Mismatch error with respect to this line

                              If objRS("Question _ID") <> strCurrentQuest ionID Then

                              Also, I did not declare a type for the following variables since you do
                              not have to in asp and secondly it gives me an error. I think this is
                              the cause of the type mismatch. Any ideas?

                              Dim iQuestionID As Integer
                              Dim iCheckboxID As Integer
                              Dim strCurrentQuest ionID As String




                              Ben Amada wrote:[color=blue]
                              > clinttoris@hotm ail.com wrote:
                              >[color=green]
                              > > Wow Ben,
                              > >
                              > > I would had to start changing code that I worked so hard on. What if I
                              > > wanted to implement your code to my code. This is my code. Short and
                              > > sweet. Could it not be incorporated into my code[/color]
                              >
                              > Didn't mean to overwhelm you :) I was able to basically copy the ASP
                              > code from my other post and paste it into your ASP code (see below).
                              > It is untested as I don't have a database setup -- but should work.
                              > The only other thing you'd want to do is insert the JavaScript I posted
                              > a few posts ago into your code.
                              >
                              > Good luck,
                              > Ben
                              > ---------------------------------------------
                              >
                              > <BODY leftmargin="0" rightmargin="0" marginwidth="0" topmargin="0"
                              > marginheight="0 " bottommargin="0 " bgcolor="#fffff f">
                              > <table width="60%" cellspacing="0" cellpadding="5" >
                              > <%
                              > Dim iQuestionID As Integer
                              > Dim iCheckboxID As Integer
                              > Dim strCurrentQuest ionID As String
                              > Set oConn = Server.CreateOb ject("ADODB.Con nection")
                              > oConn.Open "DSN"
                              > 'now find the quiz specified by the URL command line
                              > 'e.g. vbquiz.asp?quiz =1 means show quiz 1
                              > survey=1
                              > set objRS = oConn.Execute(" SELECT * FROM Survey WHERE Survey_ID=" &
                              > survey)
                              > %>
                              > <hr>
                              > <font size=5>
                              > <center><b><% =objRS("Descr") %></center></b>
                              > <HR>
                              > </font>
                              > <p>
                              > <form
                              > action="testSub mission.asp?Sur vey=<%=request. querystring("Su rvey") %>"
                              > method="post" name="SurveySub mitted">
                              > <%
                              > objRS.close
                              > 'now start the main loop to find all the questions
                              >
                              > set objRS = oConn.Execute(" SELECT * FROM survey_question s WHERE
                              > Survey_ID=" & survey & " ORDER BY Question_ID")
                              >
                              > If not objRS.eof Then
                              > %>
                              > <input type="hidden" name="HiddenSur veyID"
                              > value="<%=objRS ("Survey_ID")%> ">
                              > <input type="hidden" name="HiddenQue stionID"
                              > value="<%=objRS ("Question_ID") %>">
                              > <%
                              > End IF
                              > if not objRS.EOF then
                              > objRS.movefirst
                              > do
                              > response.write "<b>" & objRS("Question _ID") & ". " &
                              > objRS("Question ") & "</b><p>" & chr(13)
                              > 'now display the available options
                              > set objTxt = oConn.Execute(" SELECT * FROM
                              > survey_user_tex t WHERE
                              > Question_ID=" & objRS("Question _ID") )
                              > if not objTxt.EOF then
                              >
                              > objTxt.movefirs t
                              > Do
                              >
                              > If (objTxt("Type") ) = "radio" then
                              >
                              > If objRS("Question _ID") <> strCurrentQuest ionID Then
                              >
                              > If iQuestionID > 0 Then
                              > response.write "<input type=""hidden"" " & _
                              > "id=""cbCou nt" & iQuestionID & """ " & _
                              > "value=""" & iCheckboxID & """>"
                              > End If
                              >
                              > strCurrentQuest ionID = objRS("Question _ID")
                              > iQuestionID = iQuestionID + 1
                              > iCheckboxID = 0
                              >
                              > 'response.write "Question #" & iQuestionID & "<br>"
                              >
                              > End If
                              >
                              > iCheckboxID = iCheckboxID + 1
                              >
                              > response.write "<input type=""checkbox "" id=""" & _
                              > "chk" & iQuestionID & "_" & iCheckboxID & """ " & _
                              > "Name=""Questio n" & _
                              > objRS("Question _ID") & """ Value=""" & _
                              > objTxt("Sub_Tex t") & """>" & _
                              > objTxt("Text") & "</input><br>" & chr(13)
                              >
                              > ELSE
                              > response.write "<BR>" & objTxt("Text")& "<BR>" %><textarea
                              > name="textBoxAn swer<%=objRS("Q uestion_Id")%>" rows="2"
                              > cols="50"></textarea>
                              > <input type="hidden" name="HiddenTex tValue"
                              > value="<%=objTx t("sub_text")%> ">
                              > <%
                              > End IF
                              > objTxt.movenext
                              > loop until objTxt.EOF
                              >
                              > end if
                              > response.write "<p>"
                              > objRS.movenext
                              > loop until objRS.EOF
                              >
                              > If iQuestionID > 0 Then
                              > response.write "<input type=""hidden"" " & _
                              > "id=""cbCou nt" & iQuestionID & """ " & _
                              > "value=""" & iCheckboxID & """>"
                              > End If
                              >
                              > response.write "<input type=""hidden"" " & _
                              > "id=""QuestionC ount"" " & _
                              > "value=""" & iQuestionID & """>"
                              >
                              > end if
                              > oConn.close
                              > %>
                              > <hr>
                              > </TABLE>
                              > <TABLE>
                              > <TR>
                              > <TD COLSPAN=3 ALIGN="center"> <BR>
                              > <input type=submit value="Submit"
                              > ONCLICK="valida teCheckBoxes(); ">
                              > </TD>
                              > </TR>
                              > </TABLE>
                              > </FORM>
                              > </CENTER>[/color]

                              Comment

                              Working...