javascript validation checkbox error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karen987
    New Member
    • Mar 2007
    • 114

    #1

    javascript validation checkbox error

    The code below is for a checkbox, in a form on an asp page. I want to make it mandatory for the user to click it. The other validation statements work fine, this one causes a problem though.
    The browser returns a debug error, i don't get the validation javascript alert below, instead the browser says:


    error: document.frm.op t15. is null or not an object
    and invites me to debug.

    when i debug it points to the below code, i wonder what is wrong with it? the validation does go through, it's just that i don't get the alert, but on the server validation, it shows that it goes through

    Code:
    var checkbox = document.frm.opt15; 
    if(checkbox.checked == false) { 
       alert("I agree to abide by the terms of use."); 
       return false; }
    this is the form tag,

    Code:
        <form action="register.asp" method="post" name="frm" onSubmit="return ValidateUser();">


    and this is the part of the form where the input is entered. I might add, it draws data from the database for sarrNAME

    Code:
    <td><span style="color: #FF0000"><%=sarrNAME(14)%>:</span></td>
                   <td><input style="width: 100%;" type="checkbox" name="opt15" value="" size="40" maxlength="50" class="textbox" /></td>
    any help appreciated. Thanks in advance. Below is some code for the rest of the script, just so you can see how it is coded

    Code:
    	if(document.frm.name.value.length > 30) {  
       alert("Maximum 30 characters allowed for 'name'.")
      document.frm.name.focus() ;
       return false; }
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    the number 15 in the name may be throwing a problem try changing the name of opt15 to something like optone

    Comment

    • karen987
      New Member
      • Mar 2007
      • 114

      #3
      Originally posted by iam_clint
      the number 15 in the name may be throwing a problem try changing the name of opt15 to something like optone
      it's publishing software, and this part of the form was built in,

      i'll try fiddling with it, but i'm sceptical, as it would involve databases etc. Are you saying a number would cause a problem here?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        A checkbox is an array so you will need to loop through the checkbox elements with the same name. See link and this example.

        Comment

        • karen987
          New Member
          • Mar 2007
          • 114

          #5
          Hi acoder, thanks for tip. i;ll check out the pages, in the meantime, i've managed to get some code that works. The debug error has now gone. But if i leave opt15 unchecked on the form the form still goes through, with no alert.
          I tried checking opt15 and it went through OK, the validation i mean. The problem now is that i don't get a validation alert if opt15 is left unchecked. Instead the server side validation tells you to go back and fix it.

          I wonder how this can be fixed



          Code:
          //first check if opt15 exists
            if (document.frm.opt15) {
                var checkbox = document.frm.opt15; 
               if(checkbox.checked == false) { 
                   alert("Do you agree to abide by the code of conduct?"); 
                   return false;
                }
             }

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Actually your original code should work. If there is more than one checkbox, you have an array of checkboxes.

            Are you sure you don't have two elements with the same name or something like that?

            If you still have problems, post the full code or a link.

            Comment

            • karen987
              New Member
              • Mar 2007
              • 114

              #7
              Hi acoder,

              this is the relevant part of the form,

              Code:
                 <% If Trim(sarrACTIVE(14)) = "1" Then %>            
              		            <tr>
              		            	<td><span style="color: #FF0000"><%=sarrNAME(14)%>:</span></td>
              		            	<td><input style="width: 100%;" type="Text" name="opt15" value="true" size="40" maxlength="50" class="textbox" /></td>
              		            </tr>
              		            <% End If %>


              Below is the javascript validation function statement

              if i leave opt15 unchecked on the form the form still goes through, with no alert.
              I tried checking opt15 and it went through OK, the validation i mean. The problem now is that i don't get a validation alert if opt15 is left unchecked. Instead the server side validation tells you to go back and fix it.

              Code:
                 function ValidateUser(){
              
              //first check if opt15 exists
                if (document.frm.opt15) {
                    var checkbox = document.frm.opt15; 
                   if(checkbox.checked == false) { 
                       alert("Do you agree to abide by the code of conduct?"); 
                       return false;
                    }
                 }
              I tried this as well, but it returns a debug error, which when debugged points to the arrows , anyway, ususally these arrows shouldn't be in the javascript function,


              Code:
              <% If Trim(sarrACTIVE(14)) = "1" Then %>            
                    var checkbox = document.frm.opt15; 
                   if(checkbox.checked == false) { 
                       alert("Do you agree to abide by the code of conduct?"); 
                       return false;
                    }
              <% End If %>
              this doesn't work either

              Code:
                     var checkbox = document.frm.opt15; 
              if(checkbox.checked == false) { 
                 alert("Do you agree to abide by the code of conduct?"); 
                 return false; }

              this is the view source code as viewed from the web. as you can see, the checkbox shows here, but not on the form above, it's the way the software is written i think

              Code:
              <tr><td valign='top'>I have read the code of conduct: </td><td>
              <input style='' type='Checkbox' value='Code of Conduct' name='14' class=''>
              </td></tr>

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                If you look at the code:
                Code:
                <tr><td valign='top'>I have read the code of conduct: </td><td>
                <input style='' type='Checkbox' value='Code of Conduct' name='14' class=''>
                </td></tr>
                the name is "14", not "opt15". I don't know how that was produced, but it obviously can't find opt15 as an object because it doesn't exist.

                Comment

                Working...