Click to See Complete Forum and Search --> : syntax problems in a form


mt25
09-25-2003, 06:03 AM
hi guys,

i have a little problem in the following JS Code.
I have 44 Form elements.
The code below checks if every single element is klicked.
But the code should check, if only one of the 44 elements is clicked.
What I`m doing wrong?



-------------------------

function checkform()
{

var i = 0;
var number = 45;

while (i< number){

if (document.form.elements[i].checked == false) {

alert("Please klick...");

return false;
}
i ++;

}
}

thx
mt

Charles
09-25-2003, 07:02 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
-->
</style>

<script type="text/javascript">
<!--
function checkForm (f) {
var checked = false;
for (var i=0; i<f.elements.length; i++) {if (f.elements[i].type == 'checkbox' && f.elements[i].checked) checked = true};
if (!checked) alert ('Please check one.');
return checked;
}
// -->
</script>

<form action="" onsubmit="return checkForm(this)">
<div>
<label><input type="checkbox">Fee</label>
<label><input type="checkbox">Fie</label>
<label><input type="checkbox">Foe</label>
<label><input type="checkbox">Fum</label>
<button type="submit">Submit</button>
</div>
</form>

mt25
09-25-2003, 07:21 AM
Charles - thanks a lot - this one worked out for me (with a little modification)!
Bye and take care.
mt
-------------