www.webdeveloper.com
Results 1 to 7 of 7

Thread: Looping Radio buttons with validation

  1. #1
    Join Date
    Apr 2009
    Posts
    24

    Question Looping Radio buttons with validation

    Reposted this as I am still awaiting a response to my question



    Initial Post
    --------------------------------------------------------------------------------

    Here is my problem. Basically I am designing a quiz that validates each input as per what the user chooses and will not let them proceed without answering all questions correctly. At the moment I can do this for one question using name, however when I have multiple questions (I tried changing my script to validate by id) it only validates the first one, whichever is clicked first. At the moment I am validating onclick for ease of testing however the final quiz will validate onsubmit. Here is my code it would be great if anyone can tell my how I can get it to validate all questions (there will be more)

    <html>
    <head>


    <SCRIPT LANGUAGE="JavaScript">

    function checkAnswers()
    {
    var retval = false;
    for(i=0;i<document.multichoice.question.length;i++)
    {
    if(document.multichoice.question[i].value == "yes" && document.multichoice.question[i].checked == true)
    {
    retval = true;
    break;
    }
    }
    if(retval == false)
    alert("That is the wrong answer");
    return retval;
    }

    // End -->


    </SCRIPT>
    </head>

    <body>

    <FORM name="multichoice" action='./emlmc.php' method='post' onClick= "return checkAnswers()" >

    <center>


    <br><br>

    <b>1</b>

    <br><br>

    <div>
    <center>a)<input type="radio" value="yes" name="qu1" id="question">1 </center>
    </div>

    <div>
    <center>b)<input type="radio" value="no" name="qu1" id="question">2 </center>
    </div>
    <div>
    <center>c)<input type="radio" value="no" name="qu1" id="question">3 </center>
    </div>
    <div>
    <center>d)<input type="radio" value="no" name="qu1" id="question">4</center>
    </div>


    <br><br>

    <b>2</b>

    <br><br>

    <div>
    <center>a)<input type="radio" value="no" name="qu2" id="question">1 </center>
    </div>

    <div>
    <center>b)<input type="radio" value="no" name="qu2" id="question">2</center>
    </div>
    <div>
    <center>c)<input type="radio" value="no" name="qu2" id="question">3 </center>
    </div>
    <div>
    <center>d)<input type="radio" value="yes" name="qu2" id="question">4</center>
    </div>

    <input type="submit" name='submit' value="Submit">&nbsp;&nbsp;<input type='reset' name='reset' value='Clear Form'>

    </center>

    </FORM>

    </body>
    </html>


    and my original for one question using name

    <html>
    <head>


    <SCRIPT LANGUAGE="JavaScript">

    function checkAnswers()
    {
    var retval = false;
    for(i=0;i<document.multichoice.question.length;i++)
    {
    if(document.multichoice.question[i].value == "yes" && document.multichoice.question[i].checked == true)
    {
    retval = true;
    break;
    }
    }
    if(retval == false)
    alert("That is the wrong answer");
    return retval;
    }

    // End -->


    </SCRIPT>
    </head>

    <body>

    <FORM name="multichoice" action='./emlmc.php' method='post' onClick= "return checkAnswers()" >

    <center>


    <br><br>

    <b>1</b>

    <br><br>

    <div>
    <center>a)<input type="radio" value="yes" name="question">1 </center>
    </div>

    <div>
    <center>b)<input type="radio" value="no" name="question">2 </center>
    </div>
    <div>
    <center>c)<input type="radio" value="no" name="question">3 </center>
    </div>
    <div>
    <center>d)<input type="radio" value="no" name="question">4</center>
    </div>


    <br><br>


    <input type="submit" name='submit' value="Submit">&nbsp;&nbsp;<input type='reset' name='reset' value='Clear Form'>

    </center>

    </FORM>



    </body>
    </html>

    Thanks for any help you can give


    Resonse 1
    #2 04-16-2009, 12:18 PM
    WolfShade
    Registered User Join Date: Dec 2002
    Location: CHICAGO, baby!
    Posts: 283

    The id is for CSS, primarily; javascript uses name.


    Code:
    for(i=0;i<document.multichoice.qu1.length;i++) {
    ... blah blah blah..
    }
    for(i=0;i<document.multichoice.qu2.length;i++) {
    ... blah blah blah..
    }
    etc..^_^


    Respinse back from me
    #3 04-16-2009, 12:24 PM
    JscrptBeginner
    Registered User Join Date: Apr 2009
    Posts: 7

    thanks for reply

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

    Thanks for the reply Wolfshade

    I thought about that way but thought there must be an easier better code solution, say for example I had 300 questions then that is alot of repetitive code, so was trying to work out a way way to put it in a loop

    Thanks


    Response 2
    #4 04-17-2009, 03:53 AM
    WolfShade
    Registered User Join Date: Dec 2002
    Location: CHICAGO, baby!
    Posts: 283

    Not the greatest idea, but you could nest the FOR loops as long as all the radio button names keep the same designation format ("qu plus number").

    Code:
    for(i=1; i<[number of questions]; i++) { //Since the names start with 1, use 1-based index instead of 0-based as it would normally be for this kind of array
    for (j=1; j<document.multichoice.qu[j].length; j++) {
    .. blah blah blah ..
    }
    }^_^


    My second response to responses
    #5 04-17-2009, 10:43 AM
    JscrptBeginner
    Registered User Join Date: Apr 2009
    Posts: 7

    Thats it I think but can't get code to work

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

    Thanks Wolfshade that is what I was looking for but I can't get it to work hopefully someone can tell me why my code is not working. Thanks again

    <html>
    <head>


    <SCRIPT LANGUAGE="JavaScript">

    function checkAnswers()
    {
    var retval = false;

    for(i=1; i<[2]; i++) {

    for(j=1;j<document.multichoice.question[j].length;j++)
    {
    if(document.multichoice.question[j].value == "yes" && document.multichoice.question[j].checked == true)
    {
    retval = true;
    break;
    }
    }
    }
    if(retval == false)
    alert("That is the wrong answer");
    return retval;
    }

    // End -->


    </SCRIPT>
    </head>

    <body>

    <FORM name="multichoice" action='./emlmc.php' method='post' onClick= "return checkAnswers()" >

    <center>


    <br><br>

    <b>1</b>

    <br><br>

    <div>
    <center>a)<input type="radio" value="yes" name="question1">1 </center>
    </div>

    <div>
    <center>b)<input type="radio" value="no" name="question1">2 </center>
    </div>
    <div>
    <center>c)<input type="radio" value="no" name="question1">3 </center>
    </div>
    <div>
    <center>d)<input type="radio" value="no" name="question1">4</center>
    </div>


    <br><br>

    <b>2</b>

    <br><br>

    <div>
    <center>a)<input type="radio" value="no" name=""question2">1 </center>
    </div>

    <div>
    <center>b)<input type="radio" value="no" name="question2">2</center>
    </div>
    <div>
    <center>c)<input type="radio" value="no" name="question2">3 </center>
    </div>
    <div>
    <center>d)<input type="radio" value="yes" name="question2">4</center>
    </div>

    <input type="submit" name='submit' value="Submit">&nbsp;&nbsp;<input type='reset' name='reset' value='Clear Form'>

    </center>

    </FORM>



    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,943

    Lightbulb Something else to consider ...

    See post #6 of http://www.webdeveloper.com/forum/sh....php?t=2071066
    for an alternate solution. See how the validation sections for radio buttons and select boxes was handled as well as single and multiple checkbox clicks.

    Should be modifiable for a lot of questions, but 300 on one quiz would sure be a bummer to take, especially on-line!

    Post back if you have implementation questions.

  3. #3
    Join Date
    Apr 2009
    Posts
    24

    Link Broken

    Thanks JMRKER, unfortunately that link doesn't seem to work and I can't seem to find that thread by the number. However I actually reposted this thread again here http://www.webdeveloper.com/forum/sh...478#post999478 as I didnt seem to be getting any responses.

    As you can see in this other thread I received a response from ZeroKilled that seems to work well. Let me know if this is different from your solution as I would like to learn if there is a better way.

    Thanks

  4. #4
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    i guess s/he meant the following link, probably accidentally typed an extra '6' at the end.
    http://www.webdeveloper.com/forum/sh...d.php?t=207106
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

  5. #5
    Join Date
    Apr 2009
    Posts
    24

    resolved Sorted4sure

    Yep found it now, thanks to both of you ZeroKilled and JMRKR both solutions work great in different ways, now I just need to decide which way is better on submit or on click. Happy days, thanks again

  6. #6
    Join Date
    Apr 2009
    Posts
    24

    resolved Sorted4sure

    Yep found it now, thanks to both of you ZeroKilled and JMRKER both solutions work great in different ways, now I just need to decide which way is better on submit or on click. Happy days, thanks again

  7. #7
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,943

    Thumbs up

    Quote Originally Posted by ZeroKilled View Post
    i guess s/he meant the following link, probably accidentally typed an extra '6' at the end.
    http://www.webdeveloper.com/forum/sh...d.php?t=207106
    Yep, that was it. Guess my lighting fast fingers got away from me again!
    Thanks for the correction ...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17324 seconds
  • Memory Usage 2,933KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates