...

View Full Version : Validating a radio button



sutton silver
09-12-2008, 11:50 AM
Below linked is the last page of a 13 page form.

It carries across HIDDEN VALUES and then sends them to an ASP processor which sends to an email,

All I need is the script to validate the radio button, making it essential to be checked before the form and the values are submitted.

Simple fix anyone?

Cheers
Chris SS

http://cmschester.com/securedloanform/form13.asp

Here's my javascript at the moment

<script type="text/javascript">
<!--//
function agree(){
if(!document.forms[0].elements[4].checked){
alert('You must confirm that you are able to disclose information about this applicant to submit this application, please click the I CONFIRM button');
return false;
}
else { return true };
}
//-->
</script>


and the code for the radio button.


<input name="Submit" type="submit" class="FormBUTTON" value="Apply now" onsubmit="return agree()">


Help would be appreciated!

vwphillips
09-12-2008, 12:15 PM
<form action="processor2.asp" method="post" name="form13" target="_self" id="form13" onsubmit="return agree()">




<input name="Submit" type="submit" class="FormBUTTON" value="Apply now">

rangana
09-12-2008, 12:44 PM
Also, this part does'nt target the agreement radio button:


document.forms[0].elements[4].checked


It's the first element in the form, so it should be:


document.forms[0].elements[0].checked)


...but that is prone to error since we have to change the index when we add new element especially above the radio button.

The best way for you to do, is to give your radio button a unique identifier:


<input type="radio" name="x13_Agreement" value="Yes" id="agree">


and use this script instead:


function agree(){
if(!document.getElementById('agree').checked){
alert('You must confirm that you are able to disclose information about this applicant to submit this application, please click the I CONFIRM button');
return false;
}
else { return true };
}


Hope it helps.

sutton silver
09-12-2008, 02:29 PM
Hi Both,

Thank you very much for your response!

I have implemented your code rangana

But I still can't seem to get this to work?!

Could you please check the page again on the same link and tell me If I have missed something?

vwphillips
09-12-2008, 04:52 PM
<input type="radio" name="x13_Agreement" value="Yes" id="agree">




<form action="processor2.asp" method="post" name="form13" target="_self" id="form13" onsubmit="return Agree()">




<input name="Submit" type="submit" class="FormBUTTON" value="Apply now" >




function Agree(){
if(!document.getElementById('agree').checked){
alert('You must confirm that you are able to disclose information about this applicant to submit this application, please click the I CONFIRM button');
return false;
}
else { return true };
}


function/ids/variable names must be unique.
also suggest using a checkbox as opposed to a radio.

TwiztedIndian
12-16-2008, 05:04 AM
Hello, I tried to implement this into my script but it is not working.. I have a long form that needs multiple fields validated in addition now I need to validate some radio buttons. The script I am running now is working but I can not figure out how to get it to validate a radio button group.

Here is my current validation script

<script type="text/javascript">
<!--
function UpdateForm (obj1) {

var themessage = "You are required to complete the following fields:\n ";

if (obj1.first.value== "" ) {
themessage = themessage + " - First Name\n";
}

if (obj1.last.value== "" ) {
themessage = themessage + " - Last Name\n";
}

if (obj1.nick.value== "" ) {
themessage = themessage + " - Nick\n";
}

if (obj1.age.value== "" ) {
themessage = themessage + " - Age\n";
}

if (obj1.seat.value== "" ) {
themessage = themessage + " - Seat\n";
}

if (obj1.email.value== "" ) {
themessage = themessage + " - E-Mail\n";
}

if (obj1.address1.value== "" ) {
themessage = themessage + " - Address1\n";
}

if (obj1.city.value== "" ) {
themessage = themessage + " - City\n";
}

if (obj1.state.value== "" ) {
themessage = themessage + " - State\n";
}

if (obj1.zip.value== "" ) {
themessage = themessage + " - Zip\n";
}

if (obj1.emergencyname.value== "" ) {
themessage = themessage + " - Emergency Contact Name\n";
}

if (obj1.emergencyphone.value== "" ) {
themessage = themessage + " - Emergency Contact Phone\n";
}

if (obj1.payat.checked== "" ) {
themessage = themessage + " - Please choose a method of payment\n";
}

//alert if fields are empty and cancel form submit

if (themessage == "You are required to complete the following fields:\n " ) {
obj1.submit();
}
else {
alert(themessage);
return false;
}

obj1.os0.value = obj1.nick.value;
obj1.os1.value = obj1.seat.value;
}
-->
</script>

Here is the button array

<tr>
<td><div align="right">PayPal: </div></td>
<td><input name="payat" type="radio" value="PayPal" /></td>
</tr>
<tr>
<td><div align="right">Action City: </div></td>
<td><input name="payat" type="radio" value="Action City" /></td>
</tr>
<tr>
<td><div align="right">Chaos Computers: </div></td>
<td><input name="payat" type="radio" value="Chaos Computers" /></td>
</tr>
<tr>
<td><div align="right">Computer Wizards: </div></td>
<td><input name="payat" type="radio" value="Computer Wizards" /></td>
</tr>
<tr>
<td><div align="right">Friend paid for my seat: </div></td>
<td><input name="payat" type="checkbox" value="Paid by Friend" onclick="toggle(this);alert( 'We DO verify your payment status information.'); " /></td>
</tr>

I know the last one is a checkbox but it should still validate to true if checked I'm assuming...

Other than not being able to validate the "payat" button array it works fine. Can you maybe shed some insight on this for me? Thanks

Philip M
12-16-2008, 08:43 AM
Twizted Indian - to avoid confusion it would be best if you started your own thread.

All the radio buttons must have the same name, but your checkbox may not have the same name as the radio buttons.

To check which radio (if any) was selected:-


<script type = "text/javascript">

function chk() {
for (var i = 0; i < document.myFormName.payat.length; i++) {
if (document.myform.payat[i].checked) {
var sel = i;
}
}
if (sel) {
alert ("Radio " + sel + " selected")
}
else {
alert ("No radio button was selected");
}
}

</script>

You may wish to alter the checkbox to another radio, and then:-

if (sel == 4) {
var verify = confirm ( 'We DO verify your payment status information.');
if (verify) {
// proceed
}
else {
// cancel booking
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum