最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Radio button required - JavaScript validation - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited May 24, 2011 at 20:03 vlevsha asked May 24, 2011 at 20:00 vlevshavlevsha 1792 gold badges4 silver badges16 bronze badges 1
  • We'll also need to see the test_required() function to understand what is happening here. – Jon Cram Commented May 24, 2011 at 20:05
Add a comment  | 

5 Answers 5

Reset to default 5

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

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

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>

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.

I always recommend using jQuery validate seems better to me than trying to re-invent the wheel

发布评论

评论列表(0)

  1. 暂无评论