Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Form Validating A Radio Button

    Hi all... I have a group 2 Radio buttons, if the user selects the 2nd Radio button then they need to enter some data into a text field... The following only works for either Radio buttons... How can I specify it to be only the 2nd Radio button?

    Code:
    if(!document.form.radio1.checked && document.form.q3.value=="")
        {
          theMessage = theMessage + "\n --> You selected no we need a reason)";
        }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by tomyknoker View Post
    Hi all... I have a group 2 Radio buttons, if the user selects the 2nd Radio button then they need to enter some data into a text field... The following only works for either Radio buttons... How can I specify it to be only the 2nd Radio button?

    Code:
    if(!document.form.radio1.checked && document.form.q3.value=="")
        {
          theMessage = theMessage + "\n --> You selected no we need a reason)";
        }
    if((!document.form.radio1.checked) && document.form.q3.value=="")

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi there... But the thing is with this I have 2 Radio buttons in this group 'radio1' If the first one is selected, then the user doesn't need to fill out the textfield q3... The above code makes them fill it out wether they select the Yes OR No radio in that group...

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Instead of checking whether it is "checked", check the value of the radio button that is checked.

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ahhhh good idea, I did that but no luck... Even if I just have the radio validation part there it ignores it:

    Code:
    if((document.form.radio1.value=="No") && document.form.q3.value=="") {
    And these are my radio buttons:

    Code:
    <input type="radio" name="radio1" id="yes" value="Yes">
                                    </label>
                                  Yes&nbsp;&nbsp; 
                                  <label>
                                  <input type="radio" name="radio1" id="yes2" value="No">
                                  </label> 
                                  No

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    if((!document.form.radio1[0].checked) && document.form.q3.value=="")

    Javascript addresses each radio button using the array name and index number (starting at 0).

    You did not post all you code before.


  •  

    Posting Permissions

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