Question Details    

   Question

Time: 03:23 - Jun 20, 2007     Asked by: albertstephen123      Status: Unanswered      Points: 75   

Radio button validation

I hav a form wich has got som radio buttons.The no. of the radio button can change. I need atleast one checked. How can I do this without error as the no of radio btn varies bwn 1- any number...

 
  • Ask a New Question
  • Become a Quomon Expert
  • Current Categories

     

    Similar Questions


       
     

    Answer Discussion

    Answer Discussion

    Answer Summaries

    Answer Summary

     
     

    Question & Answer System for Websites and Corporate Intranets

  • Save your corporation money by having common business problems solved once
  • Drive a significant amount of new traffic to your website, products, and services
  • Advertisement

    Hi Albert,
    This would be my solution:

    You'll give the radiobuttons the same name and distinct non-empty values.
    That way you can simply check the input element's value, and if it is empty, no button has been selected.

    You don't need to know how many radiobuttons there are and you don't need to loop through them.

    Example:
    [html]
    <input type="radio" name="myradio" value="1" />
    <input type="radio" name="myradio" value="2" />
    <input type="radio" name="myradio" value="3" />
    <input type="radio" name="myradio" [...]
    and so on...

    [javascript]
    if (document.forms.myform.elements.myradio.value == "")
    alert("No radiobutton selected...");

    Hope you can rewrite it to suit your solution.

    Jakob

    Expert:

    jgivoni

    Date:

    Jun 22, 2007

    Time:

    02:05

     

    Votes: Good (0) | Bad (0)
    Login to rate this answer

    try this javascript function called checkRadio
    its a generic one, you simply pass in the name of your form and radio button group
    This has to be called from another javascript function
    The code example below shows a simple web page which simply tells you whether you have selected a button or not by the click of a button




    <HTML>
    <HEAD>

    <SCRIPT LaNGUAGE="JavaScript">

    function checkRadio (frmName, rbGroupName) {
     var radios = document[frmName].elements[rbGroupName];
     for (var i=0; i <radios.length; i++) {
      if (radios[i].checked) {
       return true;
      }
     }
     return false;
    }

    function valFrm() {
     if (!checkRadio("frm1","fruits"))
      alert("You didnt select any fruits");
     else
      alert("You selected a fruit");
    }
    </SCRIPT>

    </HEAD>

    <BODY>

    <FORM NAME="frm1" ACTION="javascript:valFrm();">
    <INPUT TYPE="radio" NAME="fruits" value="apples"/>Apples
    <INPUT TYPE="radio" NAME="fruits" value="pears"/> Pears
    <INPUT TYPE="radio" NAME="fruits" value="bananas"/>Bananans
    <INPUT TYPE="submit">
    </FORM>

    </BODY>
    </HTML>


    Expert:

    nidhi

    Date:

    Jun 22, 2007

    Time:

    03:56

     

    Votes: Good (0) | Bad (0)
    Login to rate this answer

    Hmm, I tried my own solution and it didn't work. I guess I was assuming something that I shouldn't...
    However, I think nidhi's answer will work and I think the points should be given :-)
    jakob

    Expert:

    jgivoni

    Date:

    Jun 28, 2007

    Time:

    15:17

     

    Votes: Good (0) | Bad (0)
    Login to rate this answer

    albertstephen123, did you get your question solved?

    If you did then please close this question and distribute the points. If you found the solution on your own, we would be very happy if you could explain it here for the sake other users having the same problem.

    If you didn't get a solution to your problem, please leave a comment here to let the experts know that you're still looking for an answer.

    Thanks,
    The Quomon Admin Team

    Expert:

    admin

    Date:

    Aug 22, 2007

    Time:

    13:25

     

    Votes: Good (0) | Bad (0)
    Login to rate this answer

    For a limited time save $10 off CA Internet Security Suite 2008

    Answer Summary Currently Unavailable.

    Answer Summaries cannot be submitted until the Answer Discussion is complete.

    If you have an Answer for this question, submit it in the Answer Discussion area.




    Respond to this question:

    New User

      Email:

    Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

    Registered User

    Username:

    Password:


    Forgotten Password

    Enter your email address below and we will resend your login information to you.

    Email:


     


     
       

    "Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it


    All Questions


    Language Options

    English:

    www.quomon.com

    Español:

    www.quomon.es

    Quomon Blog

    blog.quomon.com

    Sponsors

    Questions and Answers Software
    Real Estate Postcards
    Marketing Fulfillment