473,508 Members | 2,460 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.submit();
}

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

<form name="TEST" method="post" action="evaluation-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="ValidateTest()">
</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 2019


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

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.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="evaluation-e.cfm"
onsubmit="return validateTest(this.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="return validate(this);">

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

[snip]
<form name="TEST" method="post" action="evaluation-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><input type="radio" name="Q1" value="...">
Blah</label></li>
<li><label><input type="radio" name="Q1" value="...">
Blah</label></li>
<li><label><input type="radio" name="Q1" value="...">
Blah</label></li>
<li><label><input type="radio" name="Q1" value="...">
Blah</label></li>
</ol>
</li>
<li>Blah blah blah?
<ol>
<li><label><input type="radio" name="Q2" value="...">
Blah</label></li>
<li><label><input type="radio" name="Q2" value="...">
Blah</label></li>
<li><label><input type="radio" name="Q2" value="...">
Blah</label></li>
<li><label><input 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******@hotmail.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**********************@f14g2000cwb.googlegroups .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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.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
6129
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...
2
3126
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...
6
3259
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 ...
6
3113
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...
6
12349
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...
1
6845
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...
10
6051
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...
2
6781
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...
5
6681
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 =...
0
7231
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,...
0
7133
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...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
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...
0
5643
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,...
1
5059
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...
0
3214
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...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.