5

I know nothing of JavaScript. I had to add a group of two radio buttons to an HTML form with values "yes" and "no". Now I need to make them "required" There are several other required fields in the form and this piece of JavaScript:

    <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;
  }

It was done by someone else, not me. What I did is just added my field to this array, like this:

reqd_fields[10] = "acknowledge";

However it doesn't seem to be working.

Please guide me as I am totally ignorant when it comes to JavaScript.

1
  • We'll also need to see the test_required() function to understand what is happening here. Commented May 24, 2011 at 20:05

5 Answers 5

4

A link to your page or a sample of your HTML would make this easier, but I'm going to hazard a guess and say that the values in the array match the "name" attribute of your radio button elements.

If this the case, "acknowledge" should be the name of both radio buttons, and to make things easier, one should have the attribute "checked" set to "true" so there is a default, so you'll get a value either way.

So, something like this:

<input type="radio" name="acknowledge" value="yes" /> Yes <br/>
<input type="radio" name="acknowledge" value="no" checked="true" /> No <br/>
Sign up to request clarification or add additional context in comments.

Comments

4

Why don't you just make one selected by default then one will always be selected.

Comments

1

I know question is ancient but this is a simple solution that works.

<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>

Comments

0

Without seeing your HTML and more context of your validate function it's unclear exactly what you're looking for, but here's an example of how to require a selected value from a radio group:

<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>

Here is a working example on jsFiddle.

Comments

0

document.querySelector('input[name="my_radio_button_set"]:checked' returns null if none of the buttons are checked. This is an easy way to test if one of the buttons is selected. Furthermore, if it is an object, you can get the value selected from the value property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.