Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox validation

Status
Not open for further replies.

ibjdt

Programmer
Nov 25, 2002
63
i have a form with 10 checkboxes

Code:
<INPUT type="checkbox" value=1>.....
<INPUT type="checkbox" value=10>

i have a validation where if checkbox 10 is checked, extra info is displayed versus when it is not checked

Code:
for (i=0;i<totalOptions;i++) {
	if (document.myForm5.elements[i].checked == true) {
		isChecked = true;
		count++;
	}
}
if (document.myForm5.elements[10].checked == true) {
	extrainfo
}
if (isChecked) { 
	codetoshowalways
}

this works great if checkbox 10 is checked, but i get an error if 10 isn't checked and the js doesn't complete.

any ideas?

thanks.
 
what is the error you are getting? View the page in FF and use the "Options -> Error Console" feature - IE's error descriptions are horrible.

Also, I could be wrong here, but the 10th checkbox should be element[9] as arrays in js are zero based.

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
is this what you want it to do?

Code:
 <form name="myForm5">
<INPUT type="checkbox" value=1> 1<br />
<INPUT type="checkbox" value=2> 2<br />
<INPUT type="checkbox" value=3> 3<br />
<INPUT type="checkbox" value=4> 4<br />
<INPUT type="checkbox" value=5> 5<br />
<INPUT type="checkbox" value=6> 6<br />
<INPUT type="checkbox" value=7> 7<br />
<INPUT type="checkbox" value=8> 8<br />
<INPUT type="checkbox" value=9> 9<br />
<INPUT type="checkbox" value=10> 10<br />
 </form>
<a href="javascript:doIt();">Check It</a>
 
<script>
function doIt() {
var isChecked;
var totalOptions = 10;
var count = 0;
for (i=0;i<totalOptions;i++) {
    if (document.myForm5.elements[i].checked == true) {
        isChecked = true;
        count++;
    }
}
if (document.myForm5.elements[9].checked == true) {
    alert("cbx 10 is checked");
}
if (isChecked) { 
    alert("do something");
} else {
	alert("nothing checked");
}
}
</script>

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
thanks for the quick response.

while testing it further i realized what's happening - it's based on something i left out.

my base application is the form, but i have a 'div' tag inside the form.

there is a js sort function that re-writes the div based on dropdown selections so while i start with checkbox 1-10, depending on my sort, i may end up with 1, 2, 5.

the function i am testing can be called before or after a sort and i am trying to check whether the specific 10 box is checked - keep in mind it may not end up in the final list at all. the way it is written appears to be checking for 10 boxes in the list and if the 10th box is checked.

the error i get is

document.myForm5.elements.10.checked is null

which tells me that when there aren't 10 boxes it gets lost.
evidently my syntax isn't checking whether the

<INPUT type="checkbox" value=10>

is checked, but whether the 10th box on the page is checked.
as stated, if my sort creates less than 10 boxes the program is lost.

how can i check for the specific checkbox?
by the way, i would like to do so without applying a name to the checkboxes if at all possible to avoid other issues that might pop-up with the other functions.

thanks.

 
var numCbx = 0;
var elems = document.getElementsByTagName("input");
for (var i = 0 ; i < elems.length ; i++) {
if (elems.type == "checkbox")
numCbx++;
}

By the end, numCbx will be equal to the number of checkboxes you have in your page.




TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
thanks again.

your code may have accomplished the same thing, but i wasn't seeing it. however, it did get the thought juices flowing and i came up with this


for (i=0;i<totalOptions;i++) {
if (document.myForm5.elements.checked == true) {
if (document.myForm5.elements.value == '10') { input10check++; }
isChecked = true;
count++;
}
}
if (input10check) { extraInfo }
if (isChecked) { regularInfo }
else { errorMsg }



thanks again for the help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top