0

I am trying to return false if no radio button has been selected using the following script:

function validatefilling(){
    var a=document.getElementById("myfilling");
    if (fillingselected == 0){
        var resultloc=document.getElementById("fillingerror");
        resultloc.innerHTML =  "<span style='color:red;'>Please select a filling!<span>";
        return false;
    }
    var resultloc=document.getElementById("fillingerror");
    resultloc.innerHTML =  "";
    return true;
}

It doesn't seem to work however. I am trying to have the message "Please select a filling!" appear if nothing has been selected after the user submits the form. "fillingselected" is the value of the radio form submitted.

1 Answers1

0

Try this

if(!document.getElementById('myfilling').checked) {
    var resultloc=document.getElementById("fillingerror");
    resultloc.innerHTML =  "<span style='color:red;'>Please select a filling!<span>";
    return false;
} else {
    var resultloc=document.getElementById("fillingerror");
    resultloc.innerHTML =  "";
    return true;
}
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45