SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another Radio button validation Q

    Hi all,

    I'm using a form validation script that alerts fine for all fields, except when adding in any radio buttons.

    Can anyone see why this won't work pls, I'm outa ideas. I've tried:

    if(document.myform.transport.checked == "")

    also,

    if(document.myform.transport.checked)



    script snipped to just 2 checks for brevity ...

    <SCRIPT LANGUAGE="JavaScript">
    function checkFields() {
    missinginfo = "";
    if(document.myform.name.selectedIndex == "") {
    missinginfo += "\n - Name";
    }
    if(document.myform.transport.checked == "") { // This validation not working
    missinginfo += "\n - Transport";
    }
    if (missinginfo != "") {
    missinginfo ="\n" +
    "The following was incomplete: \n" +
    missinginfo + "\n" +
    "\nPlease enter and re-submit";
    alert(missinginfo);
    return false;
    }
    else return true;
    }
    </script>

    Other info:

    <FORM NAME="myform" ACTION="next.html" onSubmit="return checkFields();" METHOD=POST>

    <INPUT ID="RadioButton1" TYPE=RADIO NAME="transport" VALUE="Auto">Auto</P>
    <INPUT ID="RadioButton2" TYPE=RADIO NAME="transport" VALUE="Train">Train</P>

    Thanks for reading.

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My trusty book seems to indicate that radio buttons are accessed as an array i.e:

    document.myform.transport[i].checked

    so maybe the overall state cannot be determined without checking each individual radio button?

    But then I haven't done it myself so my understanding may be flawed...
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the pointer Dale - that was indeed the required fix:


    if (!(document.myform.transport[0].checked || document.myform.transport[1].checked)) {
    missinginfo += "\n - Transport";
    }

    Cheers again


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
  •