I know I am doing this right but it just doesn't work. The condition for radiobutton that if it's not checked then an alert should display. But it won't work!

<script>


function valid()
{
var element = document.forms["form"]["gender"];
var check = false;

for(i=0;i<=element.length;i++)
{
if(element[i].checked)
{
check = true;
break;
}
}

if(check == false)
{
alert("FALSE")
}
else
{
alert("TRUE");
}

}


</script>

<form name="form" onsubmit="valid()">

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

<input type="submit" name="submit" value="Submit"/>

</form>

I notice that you don't have semicolon on line 20

commented: thanks +3

Yeah you're right. But the problem lies at line 9. It should be less than only, not less than or equal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.