473,473 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

javascript validation checkbox error

114 New Member
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.opt15. 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

Expand|Select|Wrap|Line Numbers
  1. var checkbox = document.frm.opt15; 
  2. if(checkbox.checked == false) { 
  3.    alert("I agree to abide by the terms of use."); 
  4.    return false; }
this is the form tag,

Expand|Select|Wrap|Line Numbers
  1.     <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

Expand|Select|Wrap|Line Numbers
  1. <td><span style="color: #FF0000"><%=sarrNAME(14)%>:</span></td>
  2.                <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

Expand|Select|Wrap|Line Numbers
  1.     if(document.frm.name.value.length > 30) {  
  2.    alert("Maximum 30 characters allowed for 'name'.")
  3.   document.frm.name.focus() ;
  4.    return false; }
Apr 30 '07 #1
7 2989
iam_clint
1,208 Recognized Expert Top Contributor
the number 15 in the name may be throwing a problem try changing the name of opt15 to something like optone
Apr 30 '07 #2
karen987
114 New Member
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?
May 1 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
A checkbox is an array so you will need to loop through the checkbox elements with the same name. See link and this example.
May 1 '07 #4
karen987
114 New Member
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



Expand|Select|Wrap|Line Numbers
  1. //first check if opt15 exists
  2.   if (document.frm.opt15) {
  3.       var checkbox = document.frm.opt15; 
  4.      if(checkbox.checked == false) { 
  5.          alert("Do you agree to abide by the code of conduct?"); 
  6.          return false;
  7.       }
  8.    }
  9.  
  10.  
May 1 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
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.
May 2 '07 #6
karen987
114 New Member
Hi acoder,

this is the relevant part of the form,

Expand|Select|Wrap|Line Numbers
  1.    <% If Trim(sarrACTIVE(14)) = "1" Then %>            
  2.                     <tr>
  3.                         <td><span style="color: #FF0000"><%=sarrNAME(14)%>:</span></td>
  4.                         <td><input style="width: 100%;" type="Text" name="opt15" value="true" size="40" maxlength="50" class="textbox" /></td>
  5.                     </tr>
  6.                     <% 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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.    function ValidateUser(){
  3.  
  4. //first check if opt15 exists
  5.   if (document.frm.opt15) {
  6.       var checkbox = document.frm.opt15; 
  7.      if(checkbox.checked == false) { 
  8.          alert("Do you agree to abide by the code of conduct?"); 
  9.          return false;
  10.       }
  11.    }
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,


Expand|Select|Wrap|Line Numbers
  1. <% If Trim(sarrACTIVE(14)) = "1" Then %>            
  2.       var checkbox = document.frm.opt15; 
  3.      if(checkbox.checked == false) { 
  4.          alert("Do you agree to abide by the code of conduct?"); 
  5.          return false;
  6.       }
  7. <% End If %>  
this doesn't work either

Expand|Select|Wrap|Line Numbers
  1.        var checkbox = document.frm.opt15; 
  2. if(checkbox.checked == false) { 
  3.    alert("Do you agree to abide by the code of conduct?"); 
  4.    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

Expand|Select|Wrap|Line Numbers
  1. <tr><td valign='top'>I have read the code of conduct: </td><td>
  2. <input style='' type='Checkbox' value='Code of Conduct' name='14' class=''>
  3. </td></tr>
May 2 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
If you look at the code:
Expand|Select|Wrap|Line Numbers
  1. <tr><td valign='top'>I have read the code of conduct: </td><td>
  2. <input style='' type='Checkbox' value='Code of Conduct' name='14' class=''>
  3. </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.
May 2 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Stephen | last post by:
Could someone please help me with some validation. I have to write code which checks to see whether a dropdown list is populated with a value or a checkbox is checked. I want the code to run on the...
5
by: DotNetJunkies User | last post by:
1. i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis.......
0
by: Xavier Pacheco | last post by:
This is probably a simple one, but I cannot seem to find an example. I have a datagrid with each row containing Checkbox | Textbox | Validation When the checkbox column is checked for a...
1
by: avp | last post by:
Hi, We have an ASP.NET 2.0 (C#) application that has a web form with a CheckBoxList control and a CustomValidator control. The CustomValidator control is used to validate that at least one...
18
by: William | last post by:
I have the following javascript function that updates a scroll_list and sends the updated entry (with its index) to a server script ( i.e. http://mkmxg00/cgi/confirmUpload.pl ) for further...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
6
by: den 2005 | last post by:
Hi everybody, Question 1: How do you set the values from server-side to a client-side control or how do you execute a javascript function without a button click event? Question 2: How do you...
6
by: Peter Afonin | last post by:
Hello, I'm creating an application in ASP.NET 1.1. I need to check whether at least one checkbox in my datagrid has been checked. To do this, I'm using Javascript - I'm adding this code to...
5
by: Peter Afonin | last post by:
Hello, I'm not an expert in Javascript, so I'm seeking an advice. As I mentioned in my previous post, I use Javascript to check whether at least one checkbox in the datagrid has been checked....
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.