1

I am writing a validation function for a html code that was given to me for my class, but am not allowed to change the html code besides adding a head and script. I am at such an early stage that I don't know how to use jQuery yet and would like help with validation for the multiple radio buttons.

I have tried looking for the answer on this and many other sites and just can't seem to find it. I have tried multiple codes, but I suspect that all of them were made with jQuery.

The input for the html

<input type = "radio" name = "radNewsletter" value = "" />Health and Wellness<br />
<input type = "radio" name = "radNewsletter" value = "" />Creative Writing<br />
<input type = "radio" name = "radNewsletter" value = ""/>Gardening

The existing validation

    function validateForm() {
        var x = document.forms["frmNews"]["txtName"].value;
        if (x == "") {
            alert ("Name must be filled out.");
            return false;
            }
        var y = document.forms["frmNews"]["txtEmail"].value;
        if (y == "") {
            alert ("Email must be filled out.");
            return false;
            }

I was unable to get any other output than the form validating when I pressed the submit button, even when the existing validation should have stopped it.

3 Answers3

2

I found that radNewsletter is a common name in your form. In order to validate forms for radio buttons, you can use below code.

function validateForm() {
        var radios = document.getElementsByName("radNewsletter");
        var formValid = false;

        var i = 0;
        while (!formValid && i < radios.length) {
            if (radios[i].checked) formValid = true;
            i++;        
        }

        if (!formValid) alert("Must check some option!");
        console.log(formValid)
        return formValid;
    }
<input type = "radio" name = "radNewsletter" value = "" />Health and Wellness<br />
<input type = "radio" name = "radNewsletter" value = "" />Creative Writing<br />
<input type = "radio" name = "radNewsletter" value = ""/>Gardening

<br />
<button onclick="validateForm()">Validate
</button>
Mohan Rajput
  • 634
  • 9
  • 21
Mayur
  • 2,583
  • 16
  • 28
0
function validateForm() {

        var radios = document.getElementsByName("radNewsletter");

        if(!radios.checked)
        {
          alert("we are testing")
        }

         if(radios.checked = true){
           alert("your checking the boxs")
         }
}
vinoth s
  • 178
  • 6
0

Use document.querySelector and pusedo selector checked. This line document.querySelector('input[name="radNewsletter"]:checked') will give the first radio button with the name radNewsletter which is checked. On click of button check if this value is not null. Hopefully you can validate using this

function validate() {

  let isChecked = document.querySelector('input[name="radNewsletter"]:checked');
  if (isChecked !== null) {
    console.log(isChecked.value);
  }
}
<input type="radio" name="radNewsletter" value="hw">Health and Wellness<br />
<input type="radio" name="radNewsletter" value="cw">Creative Writing<br />
<input type="radio" name="radNewsletter" value="g">Gardening<br/>


<button type='button' onclick='validate()'>Validate</button>
brk
  • 48,835
  • 10
  • 56
  • 78