1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Ensuring that only one checkbox is selected in a form

Discussion in 'C#' started by Nagglfar, Oct 27, 2008.

  1. #1
    Without changing the format of selectors (ie. radio buttons, checkboxes, etc.) I need to use only checkboxes for this format.. and only one can be selected. What should I add to my code

    <td>Administrator
    <input type="checkbox" name="admincheckbox" value="checkbox"> &nbsp;&nbsp;Staff
    <input type="checkbox" name="staffcheckbox" value="checkbox"></td>
    </tr>

    Thats code for the two checkboxes.. is there any syntax used to only allow one checkbox to be selected of the two
     
    Nagglfar, Oct 27, 2008 IP
  2. tfraymond

    tfraymond Active Member

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    68
    #2
    The *right* way would be to use radio buttons -- that's way they exist. It also serves as a visual to the end user that 'hey, only 1 of these can be chosen.' Is there some reason that you don't want to do this?
     
    tfraymond, Oct 27, 2008 IP
  3. Nagglfar

    Nagglfar Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The boss said so, pretty much. Radio buttons IS the easier way, I am aware of that fact. But, for the sake of learning we have to provide code so only one checkbox can be selected.
     
    Nagglfar, Oct 27, 2008 IP
  4. ranabra

    ranabra Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you know JS than this will clear it up

    var checkbox_selected = "";

    function CheckIfcheckboxButtonsSelected()
    {
    if (checkbox_selected == "")
    {
    // Not checked yet
    }
    else
    {
    alert("sorry, checked already");
    }
    }


    <INPUT TYPE="checkbox" NAME="chb" onClick="checkbox_selected='SomeValue'">YES
    <INPUT TYPE="checkbox" NAME="chb" onClick="checkbox_selected='SomeValue'">No
    <INPUT TYPE="checkbox" NAME="chb" onClick="checkbox_selected='SomeValue'">Maybe

    <INPUT TYPE="button" VALUE="Check Form Fields" onClick="CheckIfcheckboxButtonsSelected()">
     
    ranabra, Oct 27, 2008 IP
  5. QuangVo

    QuangVo Peon

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    checkbox is meant for multiple selection and radio list is for a single select which is what you're after.
     
    QuangVo, Oct 28, 2008 IP