Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help. Function validation for radio button
Old 07-12-2006, 08:30 PM Help. Function validation for radio button
Junior Talker

Posts: 1
Name: Theo
Trades: 0
Hi there,

I have a php page with a mail form. All form fields are checked if the applicant has entered a value.

Example:
{
if(document.registerform.username.value=="")
{
alert("Username Field is empty!");
document.registerform.username.focus();
return false;

This works fine. But ... I also have a couple of radio buttons where people have to choose a membership plan. How do I check if a radiobutto has been "checked" and prevent continuation if this is not the case?

Help would be greatly appreciated.

Kind regards,

Theo

p.s. Don't be angry if I don't react right away because I will be on holiday for three weeks in a couple of days.
lowlands is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-12-2006, 09:26 PM Re: Help. Function validation for radio button
AliKat's Avatar
Extreme Talker

Latest Blog Post:
Save the Children
Posts: 176
Location: MS
Trades: 0
the same way..

if(document.registerform.radioname.value==""){
// stuff here
}

make sure you name all the membership plan radios the same thing and have a value associated with each one.

One way to make sure they have it checked tho if it's required is to add checked inside your tag like this

<input type="radio" name="radio" value='basic" checked />
AliKat is offline
Reply With Quote
View Public Profile Visit AliKat's homepage!
 
Old 07-12-2006, 11:03 PM Re: Help. Function validation for radio button
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
You seem to be using Javascript and not PHP.

It's always a good idea to check clientside but the same checks should be done serverside too.

I'll just write some HTML with Javascript showing a possible way of doing it, I'm not sure if this would be how I would do it but it'd give you some idea of what you could do.

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
    <title>Javascript Radio Validation</title>
    <script type="text/javascript">
    //<![CDATA[
    function validateRadio()
    {
      var yes = null;
      var no = null;
      with(document)
      {
        yes = getElementById('confirmYes');
        no = getElementById('confirmNo');
      }
      if(yes.checked && yes.value == 'yes')
      {
        alert('yes is checked');
        return true;
      }
      else if(no.checked && no.value == 'no')
      {
        alert('no is checked');
        return true;
      }
      else if(!(yes.checked || no.checked))
      {
        alert('nothing is checked');
        return false;
      }
      else
      {
        alert('how did it get to this?');
        return false;
      }
    }
    //]]>
    </script>
  </head>
  <body>
    <form action="" method="post" onsubmit="javascript:validateRadio();">
      <div>
        <label for="confirmYes">Yes <input id="confirmYes" name="confirmation" type="radio" value="yes" /></label>
        <label for="confirmNo">No <input id="confirmNo" name="confirmation" type="radio" value="no" /></label>
        <br />
        <input id="submit" name="submit" type="submit" value="Submit" />
      </div>
    </form>
  </body>
</html>
Hopefully that covers what you're looking for, if you have any questions about this then do ask. If you'd like to know how to verify the results using PHP too, then ask and I'll include that too.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.

Last edited by mastercomputers; 07-12-2006 at 11:06 PM..
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to Help. Function validation for radio button
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22236 seconds with 12 queries