473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Validating form using radio buttons and checkboxes

Hi, I recently read this article on About.com on how to create
required fields for a form:
http://javascript.about.com/library/...rmvalidate.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!
sean
Jul 23 '05 #1
1 6112
sb*****@gmail.com (sman) wrote in message news:<52************************@posting.google.co m>...
Hi, I recently read this article on About.com on how to create
required fields for a form:
http://javascript.about.com/library/...rmvalidate.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?

The functions in blformvalidate.htm validate text fields. The
included functions are not much help for check boxes nor radio
buttons.

I have created an example html file that works with check boxes and
radio buttons.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Check form fields</title>

<style type="text/css">
/* Skip displaying a bullet with a list item. */
#mailto li {
list-style: none;
}
</style>

<script type="text/javascript">

function validate(x)
{
var myMail;

var myAddress = x.elements["theAddress"].value;

// Check if the user wishes to cancel the submit.
if (myAddress.indexOf("*") >= 0)
{
alert(
"What a suprise! Please correct " +
"the error in your ways." +
" Please remove the asterisk ( * ) " +
"in the name field.");

/* Returning false will cancel the submit when
returned to the onsubmit event handler. */

return false;
}

// Find whick radio button was clicked.
var myType = findValue(x.addressType);

if (myType == null)
{
alert("You need to click on home, business, or vaction.");
return false;
}

if (myAddress == "" )
{
alert("Please fill in an address");
return false;
}

if (x.localMail.checked == true)
{
myLocalMail = x.localMail.value;
}
else
{
myLocalMail = "please do not send local junk mail";
}

if (x.nationalMail.checked == true)
{
myNationalMail = x.nationalMail.value;
}
else
{
myNationalMail = "please do not send national junk mail";
}

alert("Address type is " + myType + ",\n" +
"address is " + myAddress + ",\n" +
"receiving local mail is " + myLocalMail + ",\n" +
"and receiving national mail is " +
myNationalMail + ".");

return true;
}

// See which radio button is selected and return its value
function findValue(obj)
{
var i, theValue;
theValue = null; // null is return if no radio button
// was clicked.

/* Radio buttons with the same name form an array. The button
which is pressed will have the checked variable set to true. */

for (i=0;i<obj.length;i++)
{
if (obj[i].checked == true)
{
theValue = obj[i].value;
break;
}
}

return theValue;
}
</script>
</head>
<body>
<p>
The JavaScript in this html file shows how to determine which
radio button is selected, how to cancel a submit, and find the
value of a checked box.</p>
<p>
To cancel the submit, place an asterisk ( * ) in the address field.
</p>
<form name="myForm"
action="http://www.natAValidWebAddress.com"
method="POST"
onsubmit="return validate(document.forms['myForm']);">
<p>How would you describe the address?
<ul id="mailto">
<li>
<INPUT TYPE="radio" NAME="addressType" VALUE="home">Home
</li>
<li>
<INPUT TYPE="radio" NAME="addressType" VALUE="business">Business
</li>
<li>
<INPUT TYPE="radio" NAME="addressType" VALUE="vacation">Vacation
</li>
</ul>
</p>
<p>Address:&nbsp;
<input type="text" name="theAddress" size="40"></p>
<p>
Do you wish to receive local junk mail?
<INPUT TYPE="checkbox"
NAME="localMail"
VALUE="recieve local mail">
</p>
<p>
Do you wish to receive national junk mail?
<INPUT TYPE="checkbox"
NAME="nationalMail"
VALUE="recieve national mail">
</p>
<br>
<input type="submit" value="Submit address information">
</form>
</body>
</html>
Jul 23 '05 #2

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

Similar topics

3
by: Emanuel Marciniak | last post by:
Hi all, We have the form which uses checkboxes for several fields and the target action points to outside webservice. Unfortunatelly they do not support checkboxes. How to pass it as a radio...
7
by: JDS | last post by:
Hi, all. I'd like to do the following, preferably *without* resorting to JavaScript: I have a long, dynamically-generated form questionnaire. Not all of the form fields are dynamically...
2
by: Pat Scott | last post by:
I am looking for a general purpose javascript snippet that enables me to <div> or <span> HTML to make portions of the form be hidden and then appear. The form contains about 12 sections and some...
2
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
3
by: Garmt de Vries | last post by:
On the website of the Dutch Jules Verne Society (www.jules-verne.nl), we have several forms that visitors can use to order something, or to apply for membership. Of course, a form's primary purpose...
6
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...
24
by: Mike Otten | last post by:
Any help greatly appreciated. The validated page is at: http://myweb.stedwards.edu/michaelo/ddtab.htm The trouble is with the radio buttons (2/3-down the left column). The radios are...
5
by: Leszek | last post by:
Hi I've got html form and i want to validate it using javascript I have somerthing like this: <p> <b>Gender: <font color="red">*</font></b> <input type="radio" value="Male"...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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,...
0
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...

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.