PDA

View Full Version : trying to solve this out more than 2 days now! heelp



hantz
06-25-2008, 07:16 PM
how can i fix it?
it has to validate id user picked more than 2 checkboxes.



function validate(f)
{
len = f.elements.length;
var i=0;
var count=0;
for(i=0; i<len ; i++)
{
if(f.elements[i].type == "checkbox")
{
if (f.elements[i].checked) count++;
}
}
if (count<2) {
alert('check 2 boxes');
return false;
}
return true;
}[/CODE]

hantz
06-27-2008, 08:23 AM
meaning all the forms on the page will be validated with theat script

codeexploiter
06-27-2008, 09:32 AM
It seems that the function you've furnished seems to be a correct one. If you look at the parameter you've defined the function in a manner that it accepts the HTML form element (object). So there might be some problem if you pass the parameter as string (I mean the name of the form the way I've passed in the following program). Please do have a look at my program in which I pass the form name in which we would like to have a validation.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
</style>
<script type="text/javascript">
function validate(f){
var frm = document.forms[f];
var count = 0;
for(var i = 0; i < frm.elements.length; i++) {
if(frm.elements[i].type == 'checkbox'){
if(frm.elements[i].checked) {
count++;
}
}
}
if(count < 2){
alert('You need to check at least two checkboxes');
return false;
}else{
alert('You checked two checkboxes at least in the form. Success!!');
return true;
}
}
</script>
</head>
<body>
<form name="form1">
<div>
Field 1<input type="text" name="field1" />
</div>
<div>
Check1<input type="checkbox" name="checkbox1" value="1" />Check2 <input type="checkbox" name="checkbox2" value="2" />
</div>
<div>
Field 2 <input type="text" name="field2" />
</div>
<div>
Check3<input type="checkbox" name="checkbox3" value="3" />Check4 <input type="checkbox" name="checkbox4" value="3" />
</div>
<div>
<input type="button" onclick="validate('form1');" value="validate"/>
</div>
</form>
</body>
</html>


There is one contradiction between the way you've defined your JS function and you've mentioned in your post.


meaning all the forms on the page will be validated with theat script

With your JS function either we have to run a looping construct like for, while to achieve this effect I think.

Let me know if you need any more assistance, if possible a hyperlink would be great with your problem.

jscheuer1
06-27-2008, 01:28 PM
Another possibility is that with form elements like fieldset, label, legend - possibly others, there is no type attribute, so when testing, you would need to do:


if(f.elements[i].type && f.elements[i].type == "checkbox")

To really be on the safe side, though this shouldn't be required with valid markup, you can even do:


if(f.elements[i] && f.elements[i].type && f.elements[i].type == "checkbox")

If you want more help:

Please post a link to the page on your site that contains the problematic code so we can check it out.