Results 1 to 7 of 7

Thread: how do i validate this - radio button

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    asia
    Posts
    87

    Unhappy how do i validate this - radio button

    on my form i got four radio button and i want my user to choose this radio button before they submit it.

    what i do:
    i create a function named

    validateRadio(form)
    {
    if (form.radiobtt.checked)
    {}else
    {alert("you must choose one of this radio button");
    return false;
    }

    but with this function, i got error telling me that 'the object does'nt support this....

    what wrong and how should i code it the right way

    (question from newbie )

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    try this
    Code:
    function validateRadio(){ 
     if (!document.frm.rdo.checked)
    	alert('not checked');
     return false;
    }

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    asia
    Posts
    87
    it works but....

    this is my full function:

    for (Count = 0; Count < 2; Count++)
    {
    if (document.frm.frmHakMlk.checked)
    {}
    else
    {
    alert ("Please choose your option");
    return false;
    }

    }

    but when i click one of the radio button the result is still the same.
    it seem that when the user choose the radio button, the function cannot read it. even can't test it! what happend?

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    Do you have more than one radio button with the same name?
    if so then it's an array and should be handled:
    Code:
    document.frm.frmHakMlk[count].checked

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    asia
    Posts
    87

    Talking RESOLVED

    OPPS... silly me....

    yeah it should be that way. but i just modify it...
    not good but ok

    if ((document.frm.frmHakMlk[0].checked)|| (document.frm.frmHakMlk[1].checked)|| (document.frm.frmHakMlk[2].checked))
    {}
    else
    {
    alert ("Please choose your option");
    return false;
    }

    thanks DeadEye!

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    To make extension to more radios easier I recommend this function:
    Code:
    function IsCheckedRadio(rad, num)
    {
      var ch, i;
      for(i=0;i<num;++i)
        ch = ch || rad[i].checked;
      return ch;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    To implement this in your function you'd do
    Code:
    if(!IsCheckedRadio(document.frm.frmHakMlk, 3))
      alert("bla");
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width