Problems when validating radio buttons

Discussion in 'JavaScript' started by Bowisen, Jan 22, 2008.

  1. #1
    I'm dynamically (PHP & Mysql) building a list containing names and a radio button for each name. I'm using radio buttons because I want only only one button to be selected.

    I want a JavaScript to determine whether a button is selected or not. That's usually no problem. Let's say the name of the radio button group is 'test'. Then I can use 'document.form.test.length' to determine the number of buttons and then make a loop to see if any of them is selected.

    Now to the problem. Sometimes there is only one button in the list and my method only works for two or more. If there are three buttons the length is 3, if there are two it's 2, but if there is only one it's not 1 but 'undefined' so I can't make a loop. Any suggestions?
     
    Bowisen, Jan 22, 2008 IP
  2. webexpert

    webexpert Banned

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can make a check while getting the length for example;

    if(document.form.test.length != undefined)
    {
    //your loop code here
    }
    else
    {
    //no need to loop here, only one radio here, so check will be accordingly..
    }

    hope will be helpful..
     
    webexpert, Jan 22, 2008 IP