[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Script to make sure at least one option is ticked in a submittal form

Posted on 2008-02-10
4
Medium Priority
?
188 Views
Last Modified: 2010-04-16
I am at an end to add a code to check my form to be sure that one of the options on my form are ticked. I have been researching EE with no success. Please forward needed code to check the option buttons. I am very new to working with submittal forms.
Thanks

Robert
<script language="javascript" type="text/javascript">
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 }
function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}	
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
    }
function ValidateForm(form)
{
   if(IsEmpty(form.firstname)) 
   { 
      alert('Please enter your First Name!') 
           return false; 	  
   } 
   
   if (!isValidEmail(form.email.value)) 
   { 
      alert('Please enter correct email address!') 
         return false; 
      } 
   if(IsEmpty(form.comments)) 
   { 
      alert('Please enter your comments!') 
     // form.account_number.focus(); 
      return false; 	  
   }  
return true;
 } 
</script>
 
</HEAD>
<BODY>
<FORM action="formmail_response.cfm" method="post" onsubmit="javascript:return ValidateForm(this)">
 
<P><INPUT type=hidden value=robert@rwsmin.org name=_recipients> <INPUT 
type=hidden value="Rowsen IT Applications: " name=_subject> 
<P>Required Information is marked ( <span class="style1">*</span> ) 
<P>First Name <span class="style1">*</span>: 
  <input name="firstname" type="text"> 
  <P>Last Name:
  <input name="lastname" type="text"> 
  <P>Phone Number : 
    <input name="phonenumber" type="text"  size="20" maxlength="20">
  <P>    Email Address <span class="style1">*</span>: 
    <input name="email" size="35">  
  
  <P>    Comments <span class="style1">*</span>:<BR>
  <TEXTAREA name="comments" cols=58 rows=9></TEXTAREA>   
  <P>Option Is Required  ( <span class="style1">*</span> )   
  <P>
    <input name="InfoRequested" type="radio" value="I have no request from the list below.">
I have no request from the list below.<P>
  <input name="InfoRequested" type="radio" value="I would like information concerning foodservice.">
  I would like information concerning foodservice. <P>
  <input name="InfoRequested" type="radio" value="I would like information concerning a business application.">
  I would like information concerning a business application.<P>
    <input name="InfoRequested" type="radio" value="I would like information concerning Bible Information ">I would like information concerning Bible Information 
    <BR><br>
    <INPUT type=submit value=Submit> 
    <INPUT type=reset value=Clear>

Open in new window

0
Comment
Question by:rowsen
  • 2
4 Comments
 

Assisted Solution

by:xunrage_
xunrage_ earned 400 total points
ID: 20862817
As far as I see in your code, you don't have a default option selected for radiobox group InfoRequested. Select one one option as default and then there will be always an option selected into that radiobox group. You can do that by adding checked="checked" like in the following snipet:
<p><input name="InfoRequested" type="radio" checked="checked" value="I have no request from the list below.">I have no request from the list below.</p>
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 20864626
If you want to make sure one is checked... you can use this validation code:
function ValidateForm(form)
{
   if(IsEmpty(form.firstname)) 
   { 
      alert('Please enter your First Name!') 
           return false; 	  
   } 
   
   if (!isValidEmail(form.email.value)) 
   { 
      alert('Please enter correct email address!') 
         return false; 
      } 
   if(IsEmpty(form.comments)) 
   { 
      alert('Please enter your comments!') 
     // form.account_number.focus(); 
      return false; 	  
   }  
 
   var valchecked = false;
   for( i = 0; i < document.myform.InfoRequested.length; i++ )
   {
       if( document.myform.mygroup[i].checked == true )
       {
          valchecked = true;
          break;
       }
   }
   if(!valchecked) 
   { 
      alert('Please check one of the radio buttons!');
      return false; 	  
   }  
   
   return true;
 } 

Open in new window

0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 1600 total points
ID: 20864633
whoops, forgot to replace some variable names there ;)
function ValidateForm(form)
{
   if(IsEmpty(form.firstname)) 
   { 
      alert('Please enter your First Name!') 
           return false; 	  
   } 
   
   if (!isValidEmail(form.email.value)) 
   { 
      alert('Please enter correct email address!') 
         return false; 
      } 
   if(IsEmpty(form.comments)) 
   { 
      alert('Please enter your comments!') 
     // form.account_number.focus(); 
      return false; 	  
   }  
 
   var valchecked = false;
   for( i = 0; i < form.InfoRequested.length; i++ )
   {
       if( form.InfoRequested[i].checked == true )
       {
          valchecked = true;
          break;
       }
   }
   if(!valchecked) 
   { 
      alert('Please check one of the radio buttons!');
      return false; 	  
   }  
   
   return true;
 } 

Open in new window

0
 

Author Comment

by:rowsen
ID: 20865772
Thank you guys for your help, both these answers were very helpful.

Robert
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

611 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