Results 1 to 7 of 7

Thread: Function Not Working

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Function Not Working

    Hi there can someone take alook at the following and tell me what I am doing wrong? Basically if a user selects checkbox 2 & checkbox 3 in my form and try to submit, the can only do it if they have also selected a radio button... Just not sure if my combination is wrong...

    Code:
    myRadio = -1;
    for (i=form.radio1.length-1; i > -1; i--) {
    if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1[i].checked) {
    myRadio = i; i = -1;
    }
    }
    if (myRadio == -1) {
    theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";	
    }

  2. #2
    Join Date
    Aug 2007
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    To be honest your logic seems a little strange for some basic form validation... however please post the entire HTML document or a link to it so I can have a complete look.
    Ryan

  3. #3
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It's a confidential form so can't do that... Would this work do you think?


    Code:
    if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1.checked) {
    theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";	
    }

  4. #4
    Join Date
    Aug 2007
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Your logic says:
    If checkbox1 and checkbox2 and radio1 is checked, then display a message.

    Code:
    if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1.checked) {
    theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";	
    }
    I think you need to add a ! in front of 'form.radio1.checked' so that it only gives the message when a radio button is not selected

    Code:
    if((form.checkbox2.checked && form.checkbox3.checked) && !form.radio1.checked) {
    theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";	
    }
    Ryan

  5. #5
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ryan thanks yes that worked, but I realised it's only working for my 1st radio button, there are 5 in the group... I tried this but no luck... I thought this was saying if checkbox 2 & 3 are selected and radio1 or radio2 etc isn't... But it doesn't work...

    Code:
    if((form.checkbox2.checked && form.checkbox3.checked) && !form.radio1.checked || !form.radio2.checked) {
    theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";	
    }

  6. #6
    Join Date
    Aug 2007
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I believe this link will help you:

    http://www.quirksmode.org/js/forms.html

    It specifically describes how to see if a radio button in a group is checked.

    Ryan

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm liking the quirksmode.org updates less and less every time I read them.

    You should note that accessing form elements as document.formname.elementname is bad practice, and liable to cause conflict with other scripts. Instead, use the forms and elements collections (document.forms.formname.elements.elementname).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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