Radio button Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nitindel
    New Member
    • Dec 2007
    • 67

    Radio button Validation

    Hi all,
    Greetings...

    I have to validate the radio buttons that i have used..i mean at least one should be selected and if any one is not selected then it should give an alert and form should also not submit..

    please find the snippet i have used...

    [HTML]<form name = rate method="post" action="Rate_te st.asp">
    <input type="radio" value="A" name="u_input"> Excellent
    <input type="radio" value="B" name="u_input" >Good
    <input type="radio" value="C" name="u_input"> Average
    <input type="radio" value="D" name="u_input"> Below Average
    <br /><br />
    <input name="submit" type="submit" value="Submit" />
    </form>[/HTML]

    thanks in advance...any help will be appreciated.
    Last edited by gits; Feb 7 '08, 02:11 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    try the following example:

    [CODE=javascript]function check_radios() {
    var radios = document.getEle mentsByTagName( 'input');
    var check = false;

    for (var i = 0, node; node = radios[i]; i++) {
    if (node.type == 'radio' && node.checked) {
    check = true;
    break;
    }
    }

    return check;
    }
    [/CODE]
    it returns false when no radio-button is checked ...

    kind regards

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      There's no need to put "very very urgent" in the title. It doesn't make the problem any more urgent for the experts to answer.

      Also please enclose your posted code in [code] tags (See How to Ask a Question). Thanks!

      Moderator.

      Comment

      Working...