Radio Button Required - JavaScript Verification

I don't know anything about JavaScript. I had to add a group of two radio buttons to the HTML form with the values ​​yes and no. Now I need to make them "necessary". There are some more required fields in the form, and this JavaScript fragment:

    <SCRIPT LANGUAGE="JavaScript">
<!--
reqd_fields = new Array();
reqd_fields[0] = "name";
reqd_fields[1] = "title";
reqd_fields[2] = "company";
reqd_fields[3] = "address";
reqd_fields[4] = "city";
reqd_fields[5] = "state";
reqd_fields[6] = "zip";
reqd_fields[7] = "phone";
reqd_fields[8] = "email";
reqd_fields[9] = "employee";

function validate(form_obj) {
  if (test_required && !test_required(form_obj)) {
    return false;
  }

This was done by someone else, not me. I just added my field to this array, for example:

reqd_fields[10] = "acknowledge";

However, it does not work.

I beg you, as I am completely unaware when it comes to JavaScript.

+3
source share
5 answers

Linking to your page or a sample of your HTML will make this easier, but I'm going to guess guesses and say that the values ​​in the array correspond to the "name" attribute of the elements of your switch.

, "" , , "checked", "true", , .

, - :

<input type="radio" name="acknowledge" value="yes" /> Yes <br/>
<input type="radio" name="acknowledge" value="no" checked="true" /> No <br/>
+4

, .

+3
0

HTML , , , , :

<form name="form1">
  <input type="radio" name="foo"> Foo1<br/>
  <input type="radio" name="foo"> Foo2<br/>
</form>
<script type="text/javascript">
  var oneFooIsSelected = function() {
    var radios = document.form1.foo, i;
    for (i=0; i<radios.length; i++) {
      if (radios[i].checked) {
        return true;
      }
    return false;
  };
</script>

jsFiddle.

0

, , , .

<script type="text/javascript">
function checkForm(formname)
{
if(formname.radiobuttonname.value == '') {
alert("Error: Please select a radio button!");
return false;
}
document.getElementById('submit').value='Please wait..';void(0);
return true;
}
</script>

<form name="formname" onsubmit="return checkForm(this)"

<input type="radio" value="radio1" name="radiobuttonname" style="display:inline;"> Radio 1<br>
<input type="radio" value="radio2" name="radiobuttonname" style="display:inline;"> Radio 2<br>

<input type="submit" value="Submit">
</form>
0
source

All Articles