2 radio buttons validate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolodede
    New Member
    • Apr 2009
    • 63

    2 radio buttons validate

    i want to dont let the user submit the form unless they answer these two radio buttons questions but i dont why isnot working the code
    Code:
    unction checkForm(obj) //This fuction shows that the user need to answer Question one
    {
    var el = document.forms[0].elements;
    for(var i = 0 ; i < el.length ; ++i) {
    if(el[i].name == "att") {
    var radiogroup = el[el[i].name];
    var itemchecked = false;
    for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
    itemchecked = true;
    break;
    }
    }
    if(!itemchecked) {
    alert("Please Answer Question One");
    if(el[i].focus)
    el[i].focus();
    return false;
    }
    }
    }
    return false;
    }
    //-->
    <!--
    function Checkanswers(obj) {
    var Invalid;
    Invalid= checkForm(obj.att);
    if (Invalid)
          return (!Invalid);
    } 
    
    //-->
    </script>
    		
    </head>
    <body>
    <form id="forms" method="post" action="http://tl28serv.uws.edu.au/twainfo/form.asp" onsubmit="return Checkanswers(this);"  >
    <h1> Advanced Standing Application </h1>
    <h3>1- Personal Details </h3>
    <h4> The questions with * are mandatory so please answer them-Thank you</h4>
    <p> Are you currently enrolled at UWS?*<br/>
    <input type="radio" name="att" value="n"/> No <br/>
    <input type="radio" name="att" value="y"/> Yes then  Student ID 
    <input type="text" name="ID" size="30" maxlength="8" onblur="checkNum(this)" />  </p>
    <p> Are you an international student?* <br/>
     <input type="radio" name="int" value="N"/> No <br/>
     <input type="radio" name="int" value="Y"/> Yes </p>
    Last edited by acoder; Apr 10 '09, 03:37 PM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    I would suggest you create a function for validating radio buttons and then call that for each radio button group, e.g.
    Code:
    function radiosChecked(radios) {
        for(var i = 0; i < radios.length; i++) {
            if(radios[i].checked)
                return true;
        }
        return false;
    }
    and called:
    Code:
    if (!radiosChecked(frm.att)) {
        alert('error...');
        return false;
    }

    Comment

    • lolodede
      New Member
      • Apr 2009
      • 63

      #3
      i did as you said bur is not working why?!--
      Code:
      //This fuction shows that the user need to answer Question one and three
      function radiosChecked(obj) {
          for(var i = 0; i < radio.length; i++) {
              if(radio[i].checked)
                  return true;
          }
          return false;
      }
      
      }
      //-->
      <!--
      function Checkanswers(obj) {
      if (!radiosChecked(obj.att)) {
          alert('Pease answer question one');
          return false;
      
      }
      }
      //-->
      </script>
      		
      </head>
      <body>
      <form id="forms" method="post" action="http://tl28serv.uws.edu.au/twainfo/form.asp" onsubmit="return Checkanswers(this);"  >
      Last edited by acoder; Apr 13 '09, 11:00 AM. Reason: Added [code] tags - please use them

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        radiosChecked()
        - argument obj not used
        - variable radio undefined
        - for-loop exited in first run (doing a for-loop at all is pointless along with the return statements)

        function Checkedanswers( )
        - unknown property "att"
        - if you're only passing "this" as parameter, you may as well omit the parameter and call "this" inside the function

        Comment

        • lolodede
          New Member
          • Apr 2009
          • 63

          #5
          so what can i do to fix it?
          thanks for help

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            There are quite a few hints about what you can do, the most obvious being that you need to change obj to radio in radiosChecked. You can omit 'this' when calling checkAnswers and use 'this' within the function:
            Code:
            if (!radiosChecked(this.elements["att"])) ...

            Comment

            • lolodede
              New Member
              • Apr 2009
              • 63

              #7
              thanks for hint i get it

              Comment

              Working...