Javascript Course

In this tutorial you can learn how to check and validate radio, and checkbox buttons in JavaScript.
To verify if a radio, or checkbox button is checked, use the checked property.
If the button is checked, the JavaScript checked property returns true, otherwise returns false.

To check /validate checkbox button it's easy because each checkbox input must have an unique name.
Syntax:
if(form_name.checkbox_name.checked == false){
 alert('Checkbox button not checked');
}

It becomes a bit more complicated when it must check radio buttons, because multiple radio buttons has the same name, and JavaScript stores them into an array. So, the syntax:
form_name.radio_name
- Returns an array with all the radio buttons with name='radio_name' in the 'form_name'.
To validate the radio buttons, you must traverse that array, and check each element (each button), like in the code bellow.
var radio_buttons = form_name.radio_name;
var re = false;

// traverse the array with radio buttons, if one is checked, make 're' true and stops
for(var i=0; i<radio_buttons.length; i++){
 if(radio_buttons[i] == true){
 re = true;
 }
}

// if 're' false, means no one radio button checked
if(re == false) alert('Radio button not checked');

Example validating radio and checkbox buttons

Below it's a practical example with a form that contains two radio buttons and three checkboxes. The user must check an radio button (vegetables), and at least one checkbox (fruits), otherwise, when it submits the form, the function checkButons() alerts an error message, and returns False.
<h4>Check a vegetable, and at least one fruit</h4>
<p>- If you try to submit the form without checking the checkbox and radio button, the form is not submitted.</p>
<form action='#' method='post' name='f1' onsubmit='return checkButons(this);'>
Vegetables: <input type='radio' name='vegetable' value='cabbage' />Cabbage
 <input type='radio' name='vegetable' value='carrot' />Carrot<br>
Fruits:<br>
 <input type='checkbox' name='fruit1' value='apple' />Apple<br>
 <input type='checkbox' name='fruit2' value='pear' />Pear<br>
 <input type='checkbox' name='fruit3' value='banana' />Banana<br>
 <input type='submit' value='Submit' />
</form>

<script>
// checks buttons (radio, checkbox) - coursesweb.net
function checkButons(frm) {
 var re = false; // used to determine when a button is checked
 var err = ''; // to store the errors

 var vegetables = frm.vegetable; // contains an array with all radio buttons 'vegetable'

 // create an Array in JSonn format with checkbox buttons
 var fruits = [frm.fruit1, frm.fruit2, frm.fruit3];

 // traverse the radio buttons
 // if one is checked sets re to true, and stops the iteration with 'break'
 for(var i=0; i<vegetables.length; i++) {
 if(vegetables[i].checked == true) {
 re = true;
 break;
 }
 }

 // if 're' is false means no radio button checked, add error in 'err' 
 if(re == false) err += '- You must check at least one vegetable';

 // make 're' again False, and traverse the checkbox buttons
 // if one is checked sets re to true, and stops the iteration with 'break'
 re = false
 for(var i=0; i<fruits.length; i++) {
 if(fruits[i].checked == true) {
 re = true;
 break;
 }
 }

 // if 're' is false means no checkbox button checked, add error in 'err' 
 if (re == false) err += '\n - You must check at least one fruit';

 // if 'err' not empty, alert the error(s) and returns False to stop submitting form
 if(err != '') {
 alert(err);
 return false;
 }
 else return re;
}
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Validate radio and checkbox buttons

Last accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (69463)
  2. PHP Simple HTML DOM Parser (12452)
  3. Disable button and Enable it after specified time (17532)
  4. Get Mime Type of file or string content in PHP (6229)
  5. PHP MySQL - using MySQLi (9669)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)