473,387 Members | 1,492 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,387 software developers and data experts.

Radio button validation

Hi guys,
I'm not good at javascript. I have a series of radio buttons on my form and it's required field. One has to be checked. Do you have any javascript code for it? Please help.
Apr 1 '08 #1
1 1160
For simple forms I mostly use something like this:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title>testing</title>
  4.     <script type="text/javascript" language="JavaScript">
  5.  
  6.  
  7.       function isChecked(theInput) {
  8.         var result = false;
  9.         // for radiobuttons:
  10.         if (theInput[0] && theInput[0].type == 'radio') {
  11.           var el = theInput.length;
  12.           for (var x=0; x < el; x++) {
  13.             if (theInput[x].checked) {
  14.               result = true;
  15.               break;
  16.             }
  17.           }
  18.         // for checkboxes:
  19.         } else if (theInput.type == 'checkbox') {
  20.           if (theInput.checked) {
  21.             result = true;
  22.           }
  23.         }
  24.         return result;
  25.       }
  26.  
  27.       function validateForm(theForm) {
  28.         var result = true;
  29.         if (theForm.name == 'f1') {
  30.           // set mandatory fields
  31.           result = isChecked(theForm.r1) && isChecked(theForm.c1);
  32.         }
  33.  
  34.         if (theForm.name == 'f2') {
  35.           // do something different
  36.         }
  37.  
  38. /*debug*/alert(result);
  39.  
  40.         return result;
  41.       }
  42.  
  43.     </script>
  44.   </head>
  45.   <body>
  46.     <form name="f1" id="f1" onSubmit="validateForm(this)">
  47.       <input type="radio" name="r1" value="1"/>1<br/>
  48.       <input type="radio" name="r1" value="2"/>2<br/>
  49.       <input type="radio" name="r1" value="3"/>3<br/>
  50.       <input type="radio" name="r1" value="4"/>4<br/>
  51.       <input type="radio" name="r1" value="5"/>5<br/>
  52.       <input type="radio" name="r1" value="6"/>6<br/>
  53.       <br/><br/>
  54.       <input type="checkbox" name="c1" value="7"/>7<br/>
  55.       <br/>
  56.       <input type="submit"/>
  57.     </form>
  58.   <body>
  59. </html>
  60.  
  61.  
Apr 2 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: DettCom | last post by:
Hello, I would like to be able to display or hide fields based on whether a specific Yes/No radio button is selected. This is in conjunction with a posting a just made here in the same group...
8
by: David Cameron | last post by:
I noticed that using an HTMLInputRadioButton and specifying a value to be an empty string (""), this is overridden by ASP.Net which set the value of the control to be the same as the ID of the...
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...
9
by: wreed | last post by:
I have a for loop seen below.... var the_form = document.getElementById(formName); for(var i=0; i<the_form.length; i++) { var temp = the_form.elements.type; if (temp == "radio") { for (x =...
1
by: Jerim79 | last post by:
I have a simple 3 page registration form. One form, one "data validation" script and one "insert into database" script. The customer bounces back and forth from the form to the verification script...
5
by: swatidesai0407 | last post by:
hi im validating radio buttons i create dis radio button in php based on some how many records of my query. i wrote a javascript to validate this buttons. wat i do is dat wen no radio button...
0
by: Gary W. Smith | last post by:
Hello, I have a simple form with a radio button group on it and I need to do some basic validation based upon which button is checked. Here is a little more detail. if button one is...
8
by: photoboy | last post by:
I have racked by brain long enough on this, so now I need the help of someone who knows what they are doing. Here is what I am trying to achieve: First, I have two radio buttons (both unchecked)...
4
by: pureadrenaline | last post by:
Hey Guys, Please could anyone help me out with the following form I need to create a validation on the email field only if the user checked the radio button named Email. Thanks in advance. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.