...

View Full Version : Javascript Function Not Working for Radio buttons



horseridingjo
11-03-2012, 09:09 PM
Hi,
I have the following code within my html form. I can get the Javascript validation working for the rest of the form but not the radio buttons. Its not processing the function. Please advise. Go easy, I try my best! ;)


Code:

<form action="" method="post" onsubmit="return ValidateForm(this);"
id="myform">

<script type="text/javascript">
function ValidateForm(frm) {
if (frm.Tevent.value == "") {alert('Type of event is required.');frm.Tevent.focus();return false;}
if (frm.Devent.value == "") {alert('Date of event is required.');frm.Devent.focus();return false;}
if (frm.CateringType.value == "") {alert('Please tell us the type of catering you require.');frm.CateringType.focus();return false;}

//validate radio buttons
mybuttons = -1;
for (i=myform.NumberP.length-1; i > -1; i--) {
if (my.NumberP[i].checked) {
mybuttons = i; i = -1;
}
}
if (mybuttons == -1) {
alert("Please enter number of people that catering is required for.");
return false;frm.NumberP.focus();
}
thisform.submit();
}

</script>

<p align="left"><font face="Arial, Helvetica, sans-serif" color="orange">Type of Event:
<input type="text" name="Tevent" value="" size="50" />
</font> </p>
<p align="left"><font face="Arial, Helvetica, sans-serif" color="orange">Date of Event:
<input type="text" name="Devent" value="" size="20" />
</font> </p>
<p align="left"><font face="Arial, Helvetica, sans-serif" color="orange">Time catering is required:
<input type="text" name="CateringReq" value="" size="40" />
</font> </p>
<p align="left"><font face="Arial, Helvetica, sans-serif" color="orange">Number of people catering for:<br><br>
<input type="radio" name="NumberP" value="0-25">0-25
<input type="radio" name="NumberP" Value="26-50">26-50</font> </p>

<p align="right"><font face="Arial, Helvetica, sans-serif" align="right" color="orange">
<input type="submit" value="Submit">
</font> </p>
</form>

optimizer123
11-03-2012, 09:32 PM
Please wrap your codes into the wrap CODE #'s

This is where you go wrong:


<script type="text/javascript">

It should be:


<script type = "text/javascript" src = 'nameofjsfilehere (example javascript.js)'>

Philip M
11-03-2012, 10:26 PM
Please wrap your codes into the wrap CODE #'s

This is where you go wrong:


<script type="text/javascript">

It should be:


<script type = "text/javascript" src = 'nameofjsfilehere (example javascript.js)'>

I am afraid that that is totally incorrect. You are not using an external .js file. optimizer123 should stay in his basement.

You do have an error - CateringType should be CateringReq to match the field name.


<form name = "myform" id="myform" action="" method="post" onsubmit="return ValidateForm(this);">

<script type="text/javascript">

function ValidateForm(frm) {
if (frm.Tevent.value == "") {alert('Type of event is required.');frm.Tevent.focus();return false;}
if (frm.Devent.value == "") {alert('Date of event is required.');frm.Devent.focus();return false;}
if (frm.CateringReq.value == "") {alert('Please tell us the type of catering you require.');frm.CateringReq.focus();return false;}

//validate radio buttons
var mybuttons = -1;
for (var i=0; i <=1; i++) {
if (document.myform.NumberP[i].checked) {
mybuttons = i;
alert (document.myform.NumberP[i].value); // the selected value
}
}

if (mybuttons == -1) {
alert ("Please select the number of people that catering is required for.");
// You can't focus on a radio buttion group
return false;
}

frm.submit();

}
</script>


"A man would do nothing, if he waited until he could do it so well that no one at all would find fault with what he has done." - Cardinal Newman



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum