Solved

Coldfusion: How to I validate radio button group on form submission

Posted on 2010-11-17
1
1,314 Views
Last Modified: 2012-05-10
I have a Coldfusion form using CFForm.  I'm using CF's validation functionality for most of the elements but I'm not able to get it working for the radio buttons.  I have two groups of radio buttons on the same page that I want to have none of the buttons selected when the user first gets to the page but each group is required to have one button selected before proceeding to the next page.  I believe this can be done with JavaScript but how to integrate it with a form already using CFForm's built in validation?

thanks in advance
0
Comment
Question by:robinsf
1 Comment
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 34162178
Here you see two methods how to check Radio buttons in your CFFORM:
<script>
function myRadioCheck(){
  theForm = document.forms.myForm;
  for(var i=0;i<theForm.myseminar.length;i++){
    if(theForm.myseminar[i].checked) return true;
  }
  alert("Select mySeminar value.");
  return false;
}


</script>
<cfform name="myForm" method="post" onSubmit="return myRadioCheck()" >

<cfinput type="radio" name="seminar" value="1" required="yes" message="Select Seminar value." > 1 
<cfinput type="radio" name="seminar" value="2" required="yes" message="Select Seminar value." > 2 
<cfinput type="radio" name="seminar" value="3" required="yes" message="Select Seminar value." > 3 
<br>

<input type="radio" name="myseminar" value="a" > A 
<input type="radio" name="myseminar" value="b" > B 
<input type="radio" name="myseminar" value="c" > C 
<br>
<cfinput name="startDate" type="datefield" label="date:" mask="mm/dd/yyyy" required="yes" />
<br>
<input type="submit" value="mySubmit" >

</cfform>


If it is not confusing to you this version gets the variable theForm passed from ColdFusion:


<script>
function myRadioCheck(theForm){
  for(var i=0;i<theForm.myseminar.length;i++){
    if(theForm.myseminar[i].checked) return true;
  }
  alert("Select mySeminar value.");
  return false;
}


</script>
<cfform name="myForm" method="post" onSubmit="return myRadioCheck(_CF_this)" >

<cfinput type="radio" name="seminar" value="1" required="yes" message="Select Seminar value." > 1 
<cfinput type="radio" name="seminar" value="2" required="yes" message="Select Seminar value." > 2 
<cfinput type="radio" name="seminar" value="3" required="yes" message="Select Seminar value." > 3 
<br>

<input type="radio" name="myseminar" value="a" > A 
<input type="radio" name="myseminar" value="b" > B 
<input type="radio" name="myseminar" value="c" > C 
<br>
<cfinput name="startDate" type="datefield" label="date:" mask="mm/dd/yyyy" required="yes" />
<br>
<input type="submit" value="mySubmit" >

</cfform>

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now