JavaScript Function to validate checkbox

multiple checkbox validation in javascript
checkbox validation in javascript w3schools
select atleast one checkbox validation in javascript
checkbox validation in javascript on button click
how to get all checked checkbox value in javascript
javascript checkbox checked event
simple checkbox validation in javascript
radio button validation in javascript

I'm trying to not allow both checkboxes to be checked at the same time. Here is my vanilla JS. I have the function already validating to return true when one is checked and false when neither are checked. Radio boxes are not an option.

function valForm() {
  var both = document.getElementById("cEmail1" & "cPhone1");
  for (var i = 1; i <= 2; i++) {
    if (document.getElementById("cEmail1").checked) {
      return true;
    } else if (document.getElementById("cPhone1").checked) {
      return true;
    } else if (both.checked) {
      return false;
    } else {
      return false;
    }
  }
}
<form action="http://severien.com/grit/formecho.php" method="post" name="contactUsForm" onsubmit="return valForm()">
  <span class="box3"><label for="cEmail" class="l5" >Contact me by email</label>
  <input class="check1" id="cEmail1" type="checkbox" name="contactbyemail"  /></span>
  <span class="box4"><label for="cPhone" class="l6">Contact me by phone</label>
  <input class="check2" id="cPhone1" type="checkbox" name="contactbyphone"  /></span> <br />
  <div class="formSubmit"><input type="submit" value="Submit" /></div>
</form>

If radio boxes really aren't an option, then there are a few issues with your code. First of all, you are checking if each of the boxes is checked, and if either of them is checked, then you are immediately returning. The second, and much larger problem, is that your both element should be undefined. The & in JavaScript is a bitwise operator, and getElementById should only return one element. Instead, you could implement the equivalent of a logical XOR like so:

function valForm(){
     return document.getElementById("cEmail1").checked != document.getElementById("cPhone1").checked; 
}

Validating checkbox fields - JavaScript, Hi, I am trying to validate a checkbox selection but my code doesn't work. Here is the code I have: <form action="form.php" method="post" onsubmit="return  Use a checkbox to convert text in an input field to uppercase: document.getElementById("fname").value = document.getElementById("fname").value.toUpperCase(); Try it Yourself »

You can't get two elements at the same time using getElementById, so you'll need to check them separately by using the && operator.

You also need to check this first, because the two if statements before this will preempt this check.

function valForm() {
  var cEmail = document.getElementById("cEmail1");
  var cPhone1 = document.getElementById("cPhone1");
  if (cEmail.checked && cPhone1.checked) {
    console.log("false");
    return false;
  } else if (cEmail.checked || cPhone1.checked) {
    console.log("true");
    return true;
  } else {
    console.log("false");
    return false;
  }
}
<form action="http://severien.com/grit/formecho.php" method="post" name="contactUsForm" onsubmit="return valForm()">
  <span class="box3"><label for="cEmail" class="l5" >Contact me by email</label>
  <input class="check1" id="cEmail1" type="checkbox" name="contactbyemail"  /></span>
  <span class="box4"><label for="cPhone" class="l6">Contact me by phone</label>
  <input class="check2" id="cPhone1" type="checkbox" name="contactbyphone"  /></span> <br />
  <div class="formSubmit"><input type="submit" value="Submit" /></div>
</form>

JavaScript - CheckBox Validation, <html> <head> <script LANGUAGE="JavaScript"> function ValidateForm(form){ ErrorText= ""; if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked​  Javascript Checkbox Validation. In the case of a checkbox multiple values can be selected so validation is done if none of the checkboxes are ticked. You can also validate by requiring the user to select a minimum of one value or two value according to your requirement. The first example shows how to validate a checkbox if nothing is checked.

This should return false if neither or both are checked:

function valForm() {
  var email = document.getElementById("cEmail1");
  var phone = document.getElementById("cPhone1")
  if (email.checked && !phone.checked || !email.checked && phone.checked) {
    console.log('ok')
    return true;
  }
  console.log('no ok')
  return false;
}
<form action="http://severien.com/grit/formecho.php" method="post" name="contactUsForm" onsubmit="return valForm()">


  <span class="box3"><label for="cEmail" class="l5" >Contact me by email</label>
<input class="check1" id="cEmail1" type="checkbox" name="contactbyemail"  /></span>
  <span class="box4"><label for="cPhone" class="l6">Contact me by phone</label>
<input class="check1" id="cPhone1" type="checkbox" name="contactbyphone"  /></span> <br />
  <div class="formSubmit"><input type="submit" value="Submit" /></div>
</form>
</div>

Validating a checkbox with HTML5 < HTML, Example. Set the checked state of a checkbox: function check() { document.​getElementById("myCheck").checked = true; } function uncheck() { document. JavaScript Validation » Introduction » Username Validation » Password Validation » Email Validation » Phone Validation » Mobile Validation » Address Validation » Radion button » CheckBox » Select Index

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.slectOne').on('change', function() {
   $('.slectOne').not(this).prop('checked', false);
   $('#result').html($(this).data( "id" ));
   if($(this).is(":checked"))
    $('#result').html($(this).data( "id" ));
   else
    $('#result').html('Empty...!');
});
});
</script>
</head>
<body>
<input type="checkbox" class="slectOne" data-id="1 selected"/>
<input type="checkbox" class="slectOne" data-id="2 selected"/>
<input type="checkbox" class="slectOne" data-id="3 selected"/>
<input type="checkbox" class="slectOne" data-id="4 selected"/>
<input type="checkbox" class="slectOne" data-id="5 selected"/>
<input type="checkbox" class="slectOne" data-id="6 selected"/>
<input type="checkbox" class="slectOne" data-id="7 selected"/>
<input type="checkbox" class="slectOne" data-id="8 selected"/>
<input type="checkbox" class="slectOne" data-id="9 selected"/>
<input type="checkbox" class="slectOne" data-id="10 selected"/>
<span id="result"></span>
</body>
</html>

here is a good example to use as well https://www.w3schools.com/code/tryit.asp?filename=FB6JK5HW3Z53

HTML DOM Input Checkbox checked Property, Good Javascript(JS) Code Examples book contains good program examples of Javascript. Dummies can easily come on the development track with Essential  I am building a mobile web app with jQuery Mobile and I want to check if a checkbox is checked. Here is my code. <script type=text/javascript> function validate(){ if

How to validate checkbox using Javascript and HTML with Example, The idea is that at least one check box should be checked. I have an onSubmit for the form and a JS function that suppose to check it but it does not work. This. Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete

Only submit if a checkbox is checked, The final function we need is for the checkbox. You can have 1 or more checkboxes on a form. We only have 1, so checking it is fairly straightforward. If you have  I am using jQuery Validation plugin to validate check box since it does not have default option, One should be selected and max two check boxes can be selected, these is the criteria. I am not gett

Javascript Form Validation - Checkboxes, Here is the JavaScript code for easy reference: function validateForm(form) { console.log("checkbox checked is ", form.agree.checked); if(!form.agree.checked)​  Validation of checkbox button using client side JavaScript. vamshi: 22-10-2011. I want a script for both the radio buttons in such a way that one text field should be displayed when I select Yes radio button and another text field should be displayed when I select No radio button.

Comments
  • take a look at this: stackoverflow.com/questions/11234622/…
  • why not using radio button?
  • why not use radio buttons ??
  • Radio boxes does exactly what you want. Why are radio boxes not an option though?
  • else if (document.getElementById("cEmail1").checked && document.getElementById("cPhone1").checked) { seems to be the most logical bit of code to use.
  • This question is not about a jQuery approach.
  • well use it, its the best approach