Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need to make a radio button mandatory on this html form

Posted on 2009-07-07
4
Medium Priority
?
639 Views
Last Modified: 2013-12-25
Hi

I have a form, I have made a checkbox mandatory and an alert comes up if it is not ticked.
I would like to do the same for a radio button (choice of 5) but cannot seem to get it to work.

thanks.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
// <![CDATA[
function validate(frm) {
if (frm.checkbox.checked==false)
{
alert("You must read and consent to our privacy policy to send this form.");
return false;
}
}
// ]]>
</script>
</head>
<body>
<form method="POST" action="captcha.php" onSubmit="return validate(form);" name="form"> 
<textarea name="user_comments" cols="75" rows="5" id="user_comments"></textarea>
</p>
<p align="left" class="bold">Where did you hear
  about us?*</p>
<p align="left">
  <input type="radio" name="source" id="source" value="radio1">
  General Internet Search</p>
<p align="left">
  <input type="radio" name="source" id="source" value="radio2">
  Online  'Find a Financial Adviser'
  
  Directory via 'unbiased.co.uk.'</p>
<p align="left">
  <input type="radio" name="source" id="source" value="radio3">
  Nursing Home Database (ucarewecare.com)</p>
<p align="left">
  <input type="radio" name="source" id="source" value="radio4">
  Care Provider (please state which one)
  <input name="care_provider_source_is" type="text" id="radio4_source_is" size="55" maxlength="55">
</p>
<p align="left">
  <input type="radio" name="source" id="source" value="radio5">
  Other (please state):
  <input name="other_source_is" type="text" id="radio5_source_is" size="75" maxlength="75">
</p>
<p class="bold">Please press the buttons below
  to confirm that you have read our <a href="#privacypolicy">privacy
  policy</a><br>
  and to submit
  your request. </p>
<p align="left"><span class="bold">
  <input type="checkbox" name="checkbox" id="checkbox" value="1">
  </span>I/We have read and consent to your privacy policy*</p>
<p align="left" class="smalltext">The fields marked * are mandatory.</p>
<table width="549" class="bigone">
  <tr class="style1">
    <td colspan="2" align="center"> Ready ? Ok, but before you click 'Send Form' please insert the same letters and numbers you see in the image on the left into the box on the right.<br /></td>
  </tr>
  <tr class="style1">
    <td width="267" align="center"><p> <img src="captcha_image.php" /></td>
    <td width="270" align="center"><input type="text" name="captcha_input" size="15"></td>
  </tr>
</table>
<p>
  <input type="submit" name="submit" value="Please Press Here to Submit Your Request">
</body>
</html>

Open in new window

0
Comment
Question by:sp0tta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:JohnSixkiller
ID: 24792881
Hi, you have 2 options. The easy way is to make some default selection by adding "checked" to html radio.

To javascript check:
var rbs = document.getElementsByName("source");
var isOK = false;
for(var i=0; i<rbs.length; i++){
  if(rbs[i].checked){
     isOK = true;
     break;
  }
}
if(!isOK) alert("Select something.");

Open in new window

0
 

Author Comment

by:sp0tta
ID: 24792921
Hi

I would prefer to make sure the checked button was chosen by the user.

thanks
0
 
LVL 11

Accepted Solution

by:
JohnSixkiller earned 2000 total points
ID: 24792952
So add my code to your function:
function validate(frm) {
  if (frm.checkbox.checked==false){
   alert("You must read and consent to our privacy policy to send this form.");
   return false;
  }
  var rbs = document.getElementsByName("source");
  var isOK = false;
  for(var i=0; i<rbs.length; i++){
   if(rbs[i].checked){
     isOK = true;
     break;
   }
  }
  if(!isOK){
    alert("Select something.");
    return false;
  }
 
}

Open in new window

0
 

Author Comment

by:sp0tta
ID: 24792966
Perfect, quick and easy

thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

721 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