jcadima
2/16/2016 - 10:16 PM

Validate Radio Buttons ( checked or blank )

Validate Radio Buttons ( checked or blank )

http://stackoverflow.com/questions/9618504/get-radio-button-value-with-javascript


<form id="onecardform" action="newcard.php" method="post" onsubmit="return validateForm2()">
<!--  other form elements  -->

  <label class="radio-inline">
  <input type="radio" id="earlyben_yes" name="earlyben" value="yes"> Yes
  </label>
  
  <label class="radio-inline">
  <input id="earlyben_no" type="radio" name="earlyben" value="no"> No
  </label>

</form>


<script>
function validateForm2() {
	// get the values
  var token    = document.forms["onecardform"]["Token"].value;
	var ssn      = document.forms["onecardform"]["social"].value;
	var MM       = document.forms["onecardform"]["MM"].value;
	var DD       = document.forms["onecardform"]["DD"].value;    
  // check radio button, and if empty set to empty string
	var earlybenRadio = document.querySelector("input[name=earlyben]:checked");
	var earlybenValue = earlybenRadio ? earlybenRadio.value : "";
	
	// other if statements ...
	
    else if ( earlybenValue == "" || earlybenValue == null  ) {
        sweetAlert("Early Access Error", "Do you receive Federal Benefits?  Please let us know if you would like receive your Federal Benefits up to 4 days early", "error");
        //document.getElementById("earlyben_no").focus();
        return false;
    }  

}  
  
</script>