Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-20-2010, 03:41 AM   PM User | #1
connollyc4
New Coder

 
Join Date: Mar 2010
Location: New Jersey USA
Posts: 74
Thanks: 11
Thanked 4 Times in 4 Posts
connollyc4 is an unknown quantity at this point
Question How to print the Value of a radio button in a javascript validation

Hello All.

What my script does is if you do not select a radio button and you hit submit an error will pop up saying please select game1. this is taken from the name of the radio button.. How can i make it so it prints out the VALUES of the 2 radio buttons. end result should print please select Baltimore Ravens vs. Cincinnati Bengals rather than please select game1.
Code:
function isChecked(radgrp)
{
var i = radgrp.length;
do
if (radgrp[--i].checked)
return true;
while (i);
return false;
}

function validateTest(els)
{
var focus_me = null, msg = "";

if (!isChecked(els.game1))
{
msg += " Game #1\n";
focus_me = focus_me || els.game1[0];
}

if (!isChecked(els.game2))
{
msg += " Game #2\n";
focus_me = focus_me || els.game2[0];
}

if (!isChecked(els.game16))
{
msg += " Game #16\n";
focus_me = focus_me || els.game16[0];
}
if (msg != "")
{
 var prefix = "\n WARNING: The following Games(s) were not selected:\n\n";
var suffix = "\nClick OK to submit your picks anyway.\n\n";
var suffix = suffix + "\n Click CANCEL to correct your picks."
var ask = confirm(prefix + msg + suffix);
if (ask) {
 if (focus_me)
 focus_me.focus();
 return true;
 }
else{
 return false;
 }
}
}

Here is the radio button. If you dont select the radio button i want the javascript validation pop but to say "please select Baltimore Ravens vs. Cincinnati Bengals rather than game1 like it does now.
Code:
<INPUT TYPE=RADIO NAME="game1" VALUE="Baltimore Ravens">Baltimore Ravens<BR>

<INPUT TYPE=RADIO NAME="game1" VALUE="Cincinnati Bengals">Cincinnati Bengals<BR>
Thanks
connollyc4 is offline   Reply With Quote
Old 09-20-2010, 03:56 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Have you never heard of indenting your code for readability?? Really, you will do yourself (and those reading your code) a big favor.

Changes are in red.

Code:
function isChecked(radgrp)
{
    for ( i = 0; i < radgrp.length; ++i )
    {
        if (radgrp[--i].checked) return true;
    }
    return false;
}

function pair(radgrp)
{
    return radgrp[0].value + " vs. " + radgrp[1].value;
}

function validateTest(els)
{
    var focus_me = null, msg = "";

    if (!isChecked(els.game1))
    {
        msg += pair(els.game1) + "\n";
        focus_me = focus_me || els.game1[0];
    }
    if (!isChecked(els.game2))
    {
        msg += pair(els.game2) + "\n";
        focus_me = focus_me || els.game2[0];
    }
    if (!isChecked(els.game16))
    {
        msg += pair(els.game16) + "\n";
        focus_me = focus_me || els.game16[0];
    }
    if (msg != "")
    {
        var prefix = "\n WARNING: The following Games(s) were not selected:\n\n";
        var suffix = "\nClick OK to submit your picks anyway.\n\n";
                   + "\n Click CANCEL to correct your picks."
        var ask = confirm(prefix + msg + suffix);
        if (ask) {
            if (focus_me)
            focus_me.focus();
            return true;
        } else{
            return false;
        }
    }
    // ??? return true, I would assume???
    return true;
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 09-20-2010 at 04:03 AM.. Reason: forgot the .value s
Old Pedant is offline   Reply With Quote
Old 09-20-2010, 04:02 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Incidentally, if you were only showing us *part* of the code...only games 1,2,16...and you really want to show *ALL* the games from 1 to 16, then do this:
Code:
function validateTest()
{
    var focus_me = null;
    var msg = "";
    var form = document.forms[0]; 
    
    for ( var game = 1; game <= 999999; ++game )
    {
        var rbg = form["game" + game];
        if ( rbg == null ) break; // no more games
        
        if ( ! rbg[0].checked && ! rbg[1].checked )
        {
            msg += rbg[0].value + " vs. " + rbg[1].value + "\n";
            focus_me = focus_me || rbg[0];
        }
    }
    if (msg != "")
    {
        var prefix = "\n WARNING: The following Games(s) were not selected:\n\n";
        var suffix = "\nClick OK to submit your picks anyway.\n\n";
                   + "\n Click CANCEL to correct your picks."
        var ask = confirm(prefix + msg + suffix);
        if (ask) {
            if (focus_me)
            focus_me.focus();
            return true;
        } else{
            return false;
        }
    }
    return true;
}
That actually will handle any number of consecutively numbered games, of course. The loop just stops when no set of numbered radio buttons are found.


I got rid of the functions since, with only two values per radio group, they really aren't needed.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
connollyc4 (09-20-2010)
Old 09-20-2010, 04:27 AM   PM User | #4
connollyc4
New Coder

 
Join Date: Mar 2010
Location: New Jersey USA
Posts: 74
Thanks: 11
Thanked 4 Times in 4 Posts
connollyc4 is an unknown quantity at this point
Thank You!

Your second post is even better. Works perfectly.
connollyc4 is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, radio button, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:00 AM.


Advertisement
Log in to turn off these ads.