2

I have one registration form and want to add validation on gender section radio buttons. My form is like

<form name="formreg" enctype="multipart/form-data" method="post">
     <input type="radio" value="male" name="gender" /> Male<br />
      <input type="radio" value="female" name="gender" /> Female<br />
      <input value="Submit" onclick="return inputval()" type="submit" />
    </form>

I use below script but not working properly ..

<script type="text/javascript">
     function inputval(){
        if(document.getElementById("gender").checked=="false")
        {
            alert("Select at least male or female.");
            return false;
        }   
    }
    </script>

unable to find problem.

4
  • Look in your javascript console and tell us the error message please Commented Mar 22, 2013 at 17:57
  • its not showing any error ... but also not working Commented Mar 22, 2013 at 17:58
  • There is no gender id in your radio buttons. Commented Mar 22, 2013 at 17:59
  • See the first answer: set an ID to the input tags because you're selecting the name for an ID, which is selecting nothing. Thus, no error. Commented Mar 22, 2013 at 18:00

6 Answers 6

2

you re getting the element By Id, and 'gender' is your input's name, not the id.
instead of

<input type="radio" value="male" name="gender" /> Male<br />

you should use

<input type="radio" value="male" id="gender" /> Male<br />
Sign up to request clarification or add additional context in comments.

1 Comment

Please.. elaborate (: Tell the OP what he/she should be doing.
1

Since you tagged this with jquery, use :checked pseudo class selector.

<script type="text/javascript">
function inputval()
{
    if($("form[name='formreg'] input[type='radio']:checked").length != 1)
    {
        alert("Select at least male or female.");
        return false;
    }
}
</script>

Comments

0

Try this with :checked:

<script type="text/javascript">
 function inputval(){
    if($(':checked').length < 0)
    {
        alert("Select at least male or female.");
        return false;
    }   
}
</script>

Comments

0

You put jQuery down, so I'll use that. document.getElementById('checked') does not exist. Even if it did, it would only apply to one element. You would have to iterate over each of the [name=gender] elements to see if any is checked. jQuery makes this trivial:

$(".inputval").on('click', function (e) {
    if (!$("[name=gender]:checked").length) {
        alert('something something gender');
        e.preventDefault();
    }
});

http://jsfiddle.net/ExplosionPIlls/s2YfU/

EDIT: non-jQuery solution is also pretty trivial since querySelectorAll('[name=gender]:checked') works too.

Comments

0

There is no need of validation.By default check one radio button

<form name="formreg" enctype="multipart/form-data" method="post">
     <input type="radio" value="male" name="gender" checked="true" /> Male<br />
      <input type="radio" value="female" name="gender" /> Female<br />
      <input value="Submit" onclick="return inputval()" type="submit" />
    </form>

Comments

0

This fiddle could give you some ideas (without jquery). Note that I checked one of the buttons so I could show you that if one is checked, it will set anyChecked to true.

<form name="formreg" enctype="multipart/form-data" method="post">
    <input type="radio" value="male" name="gender" checked="checked"/>Male
    <br />
    <input type="radio" value="female" name="gender" />Female
    <br />
    <input value="Submit" onclick="return inputval()" type="submit" />
</form>
<div id="output"></div>

var output = document.getElementById("output");
var genderInputs = document.getElementsByName("gender");
var anyChecked = false;

for (var j = 0; j < genderInputs.length; j++) {
    if (genderInputs[j].checked === true) {
        anyChecked = true;
        break;
    }
}

output.textContent = "any checked: " + anyChecked;

http://jsfiddle.net/F8XGU/1/

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.