...

View Full Version : Form Elements



dmainslie
04-23-2004, 11:46 AM
Dear Group,

I am having problems with the code below. The code works when 2 or more radio buttons are present within the form, when only 1 it skips the second for loop.

Any Ideas?

Thanks in advance

Dave

function checkForm1()
{
var myForm

myForm = document.frmUpdate
var el = document.forms[0].elements;

for(var i = 0 ; i < el.length ; ++i)
{
//alert('Start');
//alert(el.length);
if(el[i].type == "radio")
{
//alert('Radio True');
var radiogroup = el[el[i].name];
var itemchecked = false;

//alert(el[el[i].name]);
for(var j = 0 ; j < radiogroup.length ; ++j)
{
//alert('Start2');
if(radiogroup[j].checked)
{
//alert('RadioGroupChecked');
itemchecked = true;
break;
}
}

if(!itemchecked)
{
alert("Please make a selection");

if(el[i].focus)
el[i].focus();

return false;
}
}
}
var myaction="updateschedule.asp?ID=" + radiogroup[j].value;
document.forms["frmUpdate"].action = myaction;

return true;
}

Serex
04-23-2004, 12:49 PM
dont know weather it matters a whole lot but you have


for(var i = 0 ; i < el.length ; ++i)

just change to i++, also with your j loop

Willy Duitt
04-23-2004, 01:39 PM
You can try this one which I have handy.


<script type="text/javascript">
<!--//
function validate(form){
for(var i=0; i<form.elements.length; i++){
var el = form.elements[i];
var checked = false;
if(el.type.toLowerCase() == 'radio'){
var group = el.form[el.name];
if(!group.length && !group.checked){
alert('Please check radio button '+el.name.toUpperCase());
el.focus();
return false;
}
if(!group.length && group.checked){
checked = true;
}

else{
for(var j=0; j<group.length; j++)
if(checked = group[j].checked){ break };
if(!checked){
alert('Please check a radio button in group '+el.name.toUpperCase());
el.focus();
return false;
}
}
}
} return true;
}
//-->
</script>
</head>

<body>
<form action="mailto:" onsubmit="return validate(this)">
<input type='radio' name='level1' value='113'>level1<br>
<input type='radio' name='level2' value='113'>level1<br>
<input type='radio' name='level2' value='113'>level1<br>
<input type="submit" name="Submit" value="Submit">
</form>

.....willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum