#1
  1. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2004
    Posts
    33
    Rep Power
    10

    Classic ASP and Multiple Check Boxes


    Hey Guys,

    I have a situation where I have multiple checkboxes with the same name. Basically a variable number of options for a customer to choose, but they can only select one (no, I can't use a dropdown).

    I was previously using a check for "," within the returned value to see if the user selected more than one option. Unfortunately they've decided to add commas to some of the values so in cases where you only choose one but it has a comma, it still thinks you chose more than one option.

    Is there another way that I could go about this?

    Code:
    <input type="checkbox" name="x" value="A">A
    <input type="checkbox" name="x" value="B">B
    <input type="checkbox" name="x" value="C">C
    <input type="checkbox" name="x" value="A,B">A,B
    
    ASP Validation:
    x = Trim(Request("x"))
    IF instr(x, ",") > 0 THEN
      'problem
    END IF
    Thanks,

    Scott
  2. #2
  3. 0x800A0C93
    ASP Super Hero (2500 - 2999 posts)

    Join Date
    Jan 2009
    Location
    Ridin' through this world...
    Posts
    2,975
    Rep Power
    1310
    Give them different names.
    Brinkster- free development account. Not affiliated, but I sure like free. Brinkster.com
  4. #3
  5. KIS
    ASP Hero (2000 - 2499 posts)

    Join Date
    Jul 2007
    Location
    USA
    Posts
    2,099
    Rep Power
    1437
    use javascript on the client to force only one check - plenty of examples on google...

    on the server you can use the request object's count method:

    Code:
    <%
      If Request.Form <> "" Then
       x = Request.Form("x")
       c = Request.Form("x").Count
     
       If c > 1 Then
        ' checked multiple checkboxes
        ' do this
       Else
        ' do this
       End If
      End If
    %>

    Comments on this post

    • bigmike1212 agrees : check to see if request.form.count > 1. That is definitely what you want.
    Please give respect to those that helped solve an issue by clicking on the reputation icon
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0
    If the users can only select one option, you should use input type radio instead of input type checkbox. And the validation is done automatically if they input radio have all the same name.

    <input type="radio" name="x" value="A">A
    <input type="radio" name="x" value="B">B
    <input type="radio" name="x" value="C">C
    <input type="radio" name="x" value="A,B">A,B

    Comments on this post

    • Krandor agrees : That's a simpler option that fullfills the requirements.
  8. #5
  9. 0x800A0C93
    ASP Super Hero (2500 - 2999 posts)

    Join Date
    Jan 2009
    Location
    Ridin' through this world...
    Posts
    2,975
    Rep Power
    1310
    Originally Posted by joao10b
    If the users can only select one option, you should use input type radio instead of input type checkbox.
    I agree with this, but the OP stated they were anable to change the input to a dropdown, which suggests they would not be able to implement radio buttons either. That was why javascript was suggested and why I agreed with the counting the request.object collection. I too suggested a change to the input (give 'em names!) that I now think the OP can't implement.

    We'll only know if Scotty tells us, I guess.
  10. #6
  11. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2004
    Posts
    33
    Rep Power
    10
    Hey Guys,

    Thanks for the replies.

    Regarding giving them names: The number of checkbox options changes in a lot of instances (not constant) so I was looking for an option that would allow me to easily check for duplicate selections without having to crosscheck 2-10 different named variables in the code.

    Regarding radio buttons: This would be a great solution... Unfortunately we also want the ability to be able to unselect (these choices are optional) if a selection was made in error. We also don't want to add any new "unselect" button to this section of the page.

    Request.Form.x.count!!! This is exactly what I was looking for!
  12. #7
  13. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Columbus, OH
    Posts
    1,581
    Rep Power
    278
    You can give dynamic names, I did that number of times and it work well. just add hidden field with value of number of checkboxes. For getting values just
    Code:
    <input type="radio" name="x1" value="A">A
    <input type="radio" name="x2" value="B">B
    <input type="radio" name="x3" value="C">C
    <input type="radio" name="x4" value="A,B">A,B
    <input type="hidden" name="numberOfBoxes" value="4" />
    
    
    <%
    for(i=1 to i=request("numberOfBoxes"))
    
       if(request("x" & i)<>"" then
               response.write "checked value=" & request("x" & i)
      else
               response.write " NOT checked"
      END IF
      response.write "<BR />"
    Next
    %>
    GK
    __________________________________________________ _____
    if you found this post is useful click Give Rep button (bottom side on this reply ) and agree
  14. #8
  15. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    790
    Rep Power
    30
    Code:
    If Request.Form <> "" Then
       x = Request.Form("x")
       c = Request.Form("x").Count
     
       If c > 1 Then
        ' checked multiple checkboxes
        ' do this
       Else
        ' do this
       End If
      End If
    %>
    I see how this code works with checkboxes with the same name.

    How do you apply that code if you have 2 checkboxes with different names where you can response.write a message.
  16. #9
  17. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Columbus, OH
    Posts
    1,581
    Rep Power
    278
    I'm not sure what is the problem there, but

    Code:
    <input type="radio" name="x1" value="A">A
    <input type="radio" name="x2" value="B">B
    <input type="radio" name="x3" value="C">C
    <input type="radio" name="x4" value="A,B">A,B
    <input type="hidden" name="numberOfBoxes" value="4" />
    
    
    <%
    chlNum=0
    for(i=1 to i=request("numberOfBoxes"))
    
       if(request("x" & i)<>"" then
               response.write "checked value=" & request("x" & i)
               chlNum = chlNum + 1
      else
               response.write " NOT checked"
      END IF
      response.write "<BR />"
    Next
    if chlNum > 1 then
    response.write checked multiple checkboxes
    else
    response.write checked one checkboxes
    end if
    %>
  18. #10
  19. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    790
    Rep Power
    30
    I can't seem to make that code work. What I have is 2 check boxes:

    <input type="checkbox" name="name1" value="value1">

    <input type="checkbox" name="name2" value="value2">

    When it gets to the summary or post page, I want to be able to response.write/response.end the post that says, "You have 2 selections, please go back, etc".

    Thanks, I appreciate it.
  20. #11
  21. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Columbus, OH
    Posts
    1,581
    Rep Power
    278
    Code:
    <input type="radio" name="x1" value="A">A
    <input type="radio" name="x2" value="B">B
    <input type="radio" name="x3" value="C">C
    <input type="radio" name="x4" value="A,B">A,B
    <input type="hidden" name="numberOfBoxes" value="4" />
    
    
    <%
    chlNum=0
    for(i=1 to i=request("numberOfBoxes"))
    
       if(request("x" & i)<>"" then
               response.write "checked value=" & request("x" & i)
               chlNum = chlNum + 1
      else
               response.write " NOT checked"
      END IF
      response.write "<BR />"
    Next
    if chlNum > 1 then
    response.write "You have " & chlNum  & " selections, please go back"
    else
    response.write "checked one checkbox"
    end if
    %>
  22. #12
  23. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    790
    Rep Power
    30
    response.write "You have " & chlNum & " selections, please go back"

    Thanks for the reply. I knew how to do that but didn't set my "x" value correctly. All is well now.

    I would have responded quicker but couldn't get into the system. Not liking what I see so far; bring back the old format!

IMN logo majestic logo threadwatch logo seochat tools logo