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 3 of 3

Thread: Form Elements

  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form Elements

    Dear Group,

    I am having problems with the code below. The code works when 2 or more radio buttons are present within the form, when only 1 it skips the second for loop.

    Any Ideas?

    Thanks in advance

    Dave

    function checkForm1()
    {
    var myForm

    myForm = document.frmUpdate
    var el = document.forms[0].elements;

    for(var i = 0 ; i < el.length ; ++i)
    {
    //alert('Start');
    //alert(el.length);
    if(el[i].type == "radio")
    {
    //alert('Radio True');
    var radiogroup = el[el[i].name];
    var itemchecked = false;

    //alert(el[el[i].name]);
    for(var j = 0 ; j < radiogroup.length ; ++j)
    {
    //alert('Start2');
    if(radiogroup[j].checked)
    {
    //alert('RadioGroupChecked');
    itemchecked = true;
    break;
    }
    }

    if(!itemchecked)
    {
    alert("Please make a selection");

    if(el[i].focus)
    el[i].focus();

    return false;
    }
    }
    }
    var myaction="updateschedule.asp?ID=" + radiogroup[j].value;
    document.forms["frmUpdate"].action = myaction;

    return true;
    }

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    dont know weather it matters a whole lot but you have

    Code:
    for(var i = 0 ; i < el.length ; ++i)
    just change to i++, also with your j loop

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can try this one which I have handy.

    Code:
    <script type="text/javascript">
     <!--//
      function validate(form){
        for(var i=0; i<form.elements.length; i++){
         var el = form.elements[i];
         var checked = false;
          if(el.type.toLowerCase() == 'radio'){
           var group = el.form[el.name];
            if(!group.length && !group.checked){
               alert('Please check radio button '+el.name.toUpperCase());
               el.focus();
               return false;
            }
            if(!group.length && group.checked){
               checked = true;
            }
    
            else{
              for(var j=0; j<group.length; j++)
              if(checked = group[j].checked){ break };
              if(!checked){
                 alert('Please check a radio button in group '+el.name.toUpperCase());
                 el.focus();
                 return false;
              }
            }
          }
        }        return true; 
      }  
     //-->
    </script>
    </head>
    
    <body>
    <form action="mailto:" onsubmit="return validate(this)">
    <input type='radio' name='level1' value='113'>level1<br>
    <input type='radio' name='level2' value='113'>level1<br>
    <input type='radio' name='level2' value='113'>level1<br>
    <input type="submit" name="Submit" value="Submit">
    </form>
    .....willy


  •  

    Posting Permissions

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