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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form validation script problem.

    I have a form, with 2 dynamic lists in it (generated by some proprietary ASP).

    one is a list of objects with radio buttons, one is a list of objects with checkboxes (select 1 of the radio, 1 or more of the check). i cobbled together some code from googling madly to check the user had selected a radio button, and at least 1 checkbox.

    2 scripts checking the array of radio buttons/chekcboxes are being called by an onsubmit script:

    validate code:

    Code:
       if(!checkRadio(assigntest.moduleisr_id))
       {
          errors.push("Please Select a Group");  
       }
       if(!checkCheck(assigntest.enrolmentisrid))
       {
          errors.push("Please Select Learner(s)");
       }
    
       if(errors.length>0)
       {
          reportErrors(errors);
          return false;
       }
    radio check code:
    Code:
    function checkRadio(objectList)
    {
       for(var radioIndex=0; radioIndex <objectList.length; radioIndex++)
       {
          if(objectList[radioIndex].checked)
          {
             return true;
          }
       }
       return false;
    }
    checkbox check code:
    Code:
    function checkCheck(boxList)
    {
       for(var boxIndex=0; boxIndex <boxList.length; boxIndex++)
       {
          if(boxList[boxIndex].checked)
          {
             return true;
          }
       }
       return false;
    
    }
    now, the code checking the radio buttons works fine, whichever radio button is selected, but the checkbox code ignores the last checkbox on the form (form is called assigntest) consistently. if i check the last radio button, no problem. i only realised when on a test it returned only one checkbox to tick, and didnt return an error when it wasnt ticked (the idea of the validation).

    now, i'm pretty novice at JS, don't use it too often, so it's probably something very dumbly simple, but i wonder if someone can tell me what i've buggered up.

    ta.

  • #2
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, sorry, need to clarify, it appears (now) that it's only an issue if there's only 1 tick box to tick. any more than that, and my check doesn't spot the ticked box. and now i've tested it more, the same is true of checking thr radio buttons, if there's only one, it doesnt find that i've selected it. (and thus returns "please select x") for both if there's only 1 of each.

  • #3
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i've also tried
    Code:
       for(var boxIndex in boxList)
    instead of the
    Code:
     for(var boxIndex=0; boxIndex <boxList.length; boxIndex++)
    and that apparently completely failed with "Unable to get value of the property 'checked': object is null"

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    function Check(){
       var errors=[];
       if(!checked(assigntest.moduleisr_id)) {
          errors.push("Please Select a Group");
       }
       if(!checked(assigntest.enrolmentisrid)) {
         errors.push("Please Select Learner(s)");
       }
    
       if(errors.length>0) {
          alert(errors.join('\n'));
          return false;
       }
     }
    
    function checked(objectList){
       var lgth=objectList.length;
       if (!lgth){
        return objectList.checked;
       }
       for(var radioIndex=0; radioIndex <lgth; radioIndex++){
          if(objectList[radioIndex].checked){
             return true;
          }
       }
       return false;
    }
    
    /*]]>*/
    </script>
    
    </head>
    
    <body>
    <form name="assigntest" >
    <input type="radio" name="moduleisr_id" />
    <br />
    <input type="checkbox" name="enrolmentisrid" />
    <input type="checkbox" name="enrolmentisrid" />
    
    <br /><br />
    
    <input type="button" name="" value="TEST" onmouseup="Check();"/>
    </form>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,379 Times in 4,344 Posts
    The point being: You only create an array in the DOM when you have multiple objects of the same name.

    So if you only have one checkbox named "assigntest.enrolmentisrid", then in JavaScript the value of assigntest.enrolmentisrid will *NOT* be an array. And so, of course, you can't get the .length property of the non-array.

    So, as Vic showed you, if the .length property is null, then just treat the supposed list as a *single* checkbox.

    Vic does:
    Code:
       var lgth=objectList.length;
       if (!lgth){
        return objectList.checked;
       }
    I think it is much clearer and self-explanatory to write
    Code:
       var lgth=objectList.length;
       if (lgth == null){
        return objectList.checked;
       }
    But it does the same thing.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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