#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Elephant & Castle, London
    Posts
    17
    Rep Power
    0

    validation of 18 differently named groups of radiobuttons in a form


    Hello, I have a form with 18 questions, each has three possible answers, selectable by radio buttons.

    Each question (group of 3 radio buttons) has a different name tag, i.e. question1, question2 etc..up to question 18.

    I have been trying to manipulate a javascript in order that on submission, all 18 questions have been selected, otherwise the user shouldn't be forwarded to the next page: Its not quite working.
    I know I need to iterate through the form elements (all radio buttons) and check them. Any tips, please (the code follows?

    the Javascipt:

    <script language="JavaScript">
    <!--
    function radio_validate(formObj) {
    var isOK = false;
    //iterate through form elements here in for loop
    for (i=0;i<formObj.elements.length;i++) {
    currElem = formObj.elements[i]
    //if current radio button is checked, ok = true
    if (currElem.type == "radio" && currElem.checked) {
    isOK=true;
    break;
    }
    }
    if (!isOK) alert("Please answer all questions");

    return isOK;
    }
    // --> </script>



    ------------------------------------------------------

    A part of the HTML form:

    <FORM name="theform" METHOD="POST" ACTION="http://........."
    onSubmit="return radio_validate(this);">


    <INPUT TYPE="radio" Name="question1" VALUE="1">
    <INPUT TYPE="radio" Name="question1" VALUE="2">
    <INPUT TYPE="radio" Name="question1" VALUE="3">

    <INPUT TYPE="radio" Name="question2" VALUE="1">
    <INPUT TYPE="radio" Name="question2" VALUE="2">
    <INPUT TYPE="radio" Name="question2" VALUE="3">

    <INPUT TYPE="radio" Name="question3" VALUE="1">
    (etc....up to question18).


    <INPUT TYPE="submit" Name="submit-a" Value ="Results">
    </form>
    ---------------------------------------------------------------------------------


    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    19
    Try this:
    Code:
    function getCheckedRadioValue(radio) {
    	for (var i=0; i < radio.length; i++) {
    		if (radio[i].checked) return radio[i].value
    	}
    	return false
    }
    function radio_validate(formObj) {
    	var nrofquestions = 18
    	for (i=1; i<=nrofquestions;i++) {
    		if (!getCheckedRadioValue(formObj["question"+i])) {
    			alert ("Please answer all questions...")
    			return false
    		}
    	}
    	return true
    }
    Hope it helps,
    Jeroen
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Elephant & Castle, London
    Posts
    17
    Rep Power
    0

    Thank you, sir.


    Brilliant, I was just about to write out 18 different functions as I couldn't work it out myself! Ha haaa. Have a Merry Christmas!

IMN logo majestic logo threadwatch logo seochat tools logo