473,668 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating Radio Buttons in JavaScript

I am attempting to validate radio buttons in Netscape with JavaScript.

Everything works excellent in Explorer, but refuses to work in Netscape
(all versions).
=========== JAVASCRIPT CODE =============== =========

function ValidateTest()
{
var q1 = false;
var q2 = false;

for (counter = 0; counter < TEST.Q1.length; counter++)
{
if (TEST.Q1[counter].checked)
q1 = true;
}
for (counter = 0; counter < TEST.Q2.length; counter++)
{
if (TEST.Q2[counter].checked)
q2 = true;
}

if (!q1)
{
alert("Please select an answer for Question #1.");
return false;
}
else if (!q2)
{
alert("Please select an answer for Question #2.");
return false;
}

document.TEST.s ubmit();
}

=========== HTML FORM CODE =============== ==========

<form name="TEST" method="post" action="evaluat ion-e.cfm">
<ol>
<li>Question 1?<br>
<input type="radio" name="Q1" value="1">1<br>
<input type="radio" name="Q1" value="2">2<br>
<input type="radio" name="Q1" value="3">3<br>
<input type="radio" name="Q1" value="4">4<br>
<input type="radio" name="Q1" value="5">5<br> <br>
</li>
<li>Question 2?
<br><input type="radio" name="Q2" value="1">1<br>
<input type="radio" name="Q2" value="2">2<br>
<input type="radio" name="Q2" value="3">3<br>
<input type="radio" name="Q2" value="4">4<br>
<input type="radio" name="Q2" value="5">5<br> <br>
</li>
</ol>
<input type="button" value="Continue " onClick="Valida teTest()">
</form>

=============== =============== =============== =======

The JavaScript error that I receive is the following:

"TEST is not defined."

=============== =============== =============== =======

TEST is the name of my form. Is there any quick fix that I can do to
the code above to make it work in Netscape?

Sincerely,

Marc

Jul 23 '05 #1
6 2035


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>feh!</title>
<script type="text/javascript">
//<![CDATA[

function isChecked(radgr p)
{
var i = radgrp.length;
do
if (radgrp[--i].checked)
return true;
while (i);
return false;
}

function validateTest(el s)
{
var focus_me = null, msg = "";
if (!isChecked(els .Q1))
{
msg += "• Question #1\n";
focus_me = focus_me || els.Q1[0];
}
if (!isChecked(els .Q2))
{
msg += "• Question #2\n";
focus_me = focus_me || els.Q2[0];
}
if (msg != "")
{
var prefix = "\nThe following Question(s) were not answered:\n\n";
var suffix = "\nPlease correct and re-submit. Thank you.\n";
alert(prefix + msg + suffix);
if (focus_me)
focus_me.focus( );
return false;
}
return true;
}

//]]>
</script>
</head>
<body>
<form name="TEST" method="post" action="evaluat ion-e.cfm"
onsubmit="retur n validateTest(th is.elements)">
<ol>
<li>Question 1?<br>
<input type="radio" name="Q1" value="1">1<br>
<input type="radio" name="Q1" value="2">2<br>
<input type="radio" name="Q1" value="3">3<br>
<input type="radio" name="Q1" value="4">4<br>
<input type="radio" name="Q1" value="5">5<br> <br>
</li>
<li>Question 2?
<br><input type="radio" name="Q2" value="1">1<br>
<input type="radio" name="Q2" value="2">2<br>
<input type="radio" name="Q2" value="3">3<br>
<input type="radio" name="Q2" value="4">4<br>
<input type="radio" name="Q2" value="5">5<br> <br>
</li>
</ol>
<input type="submit" value="Continue ">
</form>
</body>
</html>

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #2
On 7 Dec 2004 10:26:47 -0800, sakms <ma************ **@pwgsc.gc.ca> wrote:

[snip]
Everything works excellent in Explorer, but refuses to work in Netscape
(all versions).
IE allows the name or id of an element to be a reference to that element.
Frankly its nonsense and most browsers treat it as an error. The correct
way to refer to the form is

document.forms['form-name-or-id']

or

document.forms[n]

where n is a number representing the 'n'th form in the document.

However, there's a simpler way if you modify how the function is called.

var questions = 2;

function validate(form) {var element;
for(var i = 1, j, n; i <= questions; ++i) {
/* Obtain a reference to the ith question. */
element = form.elements['Q' + i];
j = 0; n = element.length;

/* At the end of this loop, j will hold
* the index of the selected value.
*/
while((j < n) && element[j].checked) {++j;}

/* If no item is selected, j will
* equal the number of items.
*/
if(j == n) {
alert('Please select an answer for question ' + i);
return false;
}
}
return true;
}

and call it with

<form ... onsubmit="retur n validate(this); ">

This way, you don't even need to name the form[1].

[snip]
<form name="TEST" method="post" action="evaluat ion-e.cfm">
<ol>
<li>Question 1?<br>
<input type="radio" name="Q1" value="1">1<br>
<input type="radio" name="Q1" value="2">2<br>
<input type="radio" name="Q1" value="3">3<br>
<input type="radio" name="Q1" value="4">4<br>
<input type="radio" name="Q1" value="5">5<br> <br>


This should *really* be a nested list:

<ol>
<li>Blah blah blah?
<ol>
<li><label><inp ut type="radio" name="Q1" value="...">
Blah</label></li>
<li><label><inp ut type="radio" name="Q1" value="...">
Blah</label></li>
<li><label><inp ut type="radio" name="Q1" value="...">
Blah</label></li>
<li><label><inp ut type="radio" name="Q1" value="...">
Blah</label></li>
</ol>
</li>
<li>Blah blah blah?
<ol>
<li><label><inp ut type="radio" name="Q2" value="...">
Blah</label></li>
<li><label><inp ut type="radio" name="Q2" value="...">
Blah</label></li>
<li><label><inp ut type="radio" name="Q2" value="...">
Blah</label></li>
<li><label><inp ut type="radio" name="Q2" value="...">
Blah</label></li>
</ol>
</li>
</ol>

and styled with

ol {
list-style-type: decimal;
margin: 1em 0 0 2em;
padding: 0;
}
ol ol {
list-style-type: lower-alpha;
margin: 0 0 1em 1em;
}

Or, if you're not interested in having inner numbering, change the nested
lists to UL elements, and the second rule to:

ol ul {
list-style-type: none;
margin: 0 0 1em 0;
}

[snip]

Hope that helps,
Mike
[1] And you shouldn't: forms should be identified using the id attribute
unless you're supporting an old browser like NN4 which doesn't support ids.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Michael Winter wrote:

(snip)
/* At the end of this loop, j will hold
* the index of the selected value.
*/
while((j < n) && element[j].checked) {++j;} /* If no item is selected, j will
* equal the number of items.
*/


(snip)

Mike:

while((j < n) && !element[j].checked) {++j;}

like mine better, tho ;)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4
On Tue, 07 Dec 2004 19:34:02 GMT, Rob B <fe******@hotma il.com> wrote:

[snip]
Mike:

while((j < n) && !element[j].checked) {++j;}

like mine better, tho ;)


But it has the overhead of a function call. :P

An alternative way of writing it would be:

function isChecked(group ) {
var i = group.length;
while(i--) {if(group[i].checked) {return true;}}
return false;
}

which avoids the 'do'.

By the way, you didn't alter the mark-up to conform to XHTML rules. All
those BR and INPUT elements should end />. Well, the BR elements shouldn't
be there at all as I said in my post, but if they must, they should be <br
/>. :)

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
JRS: In article <11************ **********@f14g 2000cwb.googleg roups.com>
, dated Tue, 7 Dec 2004 10:26:47, seen in news:comp.lang. javascript,
sakms <ma************ **@pwgsc.gc.ca> posted :
I am attempting to validate radio buttons in Netscape with JavaScript.


Another approach would be to add a new member to every set of buttons,
default checked; put all of them at the bottom marked "Office Use Only",
and test on submit that each is now clear.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
Dr John Stockton wrote:
[...]
Another approach would be to add a new member to every set of buttons,
default checked; put all of them at the bottom marked "Office Use Only",
and test on submit that each is now clear.


And if they had style="display: none;" they can't be clicked on, saving
user confusion and defeating attempts by contrary questioners to select
them for the sole purpose of annoying the examiner...

--
Rob
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6153
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there are no instructions on how to make checkboxes and radio buttons required. I've tried adding these to my form, but I'm having no luck. Anyone know how to add radio buttons and checkboxes using the existing code mentioned on the url? Thank you!
2
3136
by: Jeff | last post by:
I'm trying to create a dynamic form that can have multiple groups of radio buttons (each group has two buttons) with the same name. Essentially, the form allows a user to enter as many names as they want. If they need to add more, they click an "add name" button and the javascript inserts another row of input boxes. Each row should have two radio buttons to indicate sex (M F). When you have multiple text input boxes with the same...
6
3281
by: Craig Keightley | last post by:
I have a page that has n number of radio groups (yes/No) how can i prevent the form being submitted if more than one radio group is not selected? By default all radio groups are unchecked The problem i am facing is that i do not know how many yes/no radio groups will be generated
6
3124
by: Blinky | last post by:
Hi all, I have a dynamically generated page that can have 1 or more radio buttons. I am using javascript with onsubmit in the form statement to make sure a radio button is selected before allowing the page to be submitted. At the moment it only works when there is 2 or more radio buttons on the page. How can I get the radio button validation working when there is only 1 radio on the page ?
6
12371
by: Advo | last post by:
Hi there. Having a few issues and cannot see a way to actually do the following. ive duplicated a simple questionnaire as shown here: http://www.hackeradio.com/questionnaire.html meaning there are 8 questions, each with 8 possible answers. Each row of the answers has its own name ie: question12_answer1 - question12_answer8
1
6876
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and grandtotal the costs ready for the selected items to be inserted back to the database. I did something like this before with Checkboxes, but Radio button have to be named the same to maintain their groupings.
10
6080
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio button with this link of code: echo 'SCRIPT language=JavaScript setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //? <snip of code>
2
6796
by: forbes | last post by:
Hi, I have a form that contains multiple groups of checkboxes that act like radio buttons (at the clients insistance). There is one text field that is required and 28 checkbox groups. Here an example of a checkbox group: <td><input type="checkbox" name="trusted" value="1" id="ck_ttt1" onclick="... <td><input type="checkbox" name="trusted" value="2" id="ck_ttt2"
5
6691
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con = ConfigurationManager.ConnectionStrings.ConnectionString; protected void Page_Load(object sender, EventArgs e) { try
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8382
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7405
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.