...

View Full Version : Function validate assistance



MarioPro
12-30-2005, 02:11 AM
Hi,
My validate() function is not working quite right since it should validate for at least on checkbox checked and even if opne is checked it will simply display the error message instead of validating. What might be wrong here?
Thanks in advance for any help

Here's the function (note, all the checkboxes have the id='chk')



function validate(){
var countChecks = 0;
if (document.forms.getElementById == "chk")
{
for (var j in document.forms.getElementById)
if (j.checked)
countChecks++;
}
if (!countChecks)
{
alert('Select some checkboxes!')
return false;
}
}

Mongus
12-30-2005, 02:50 AM
getElementById is a function not a property. It isn't possible to use it they way you're trying to.

Element id's should be unique within a document. You can't have lots of checkboxes with the same id.

If you can post your form it would be easier to help you write it correctly.

MarioPro
12-30-2005, 02:58 AM
Thanks Mongus,

The form, or better saying, the checkboxes are dinamic and generated by php query to database and I have 5 different groups, this is, five different names on checkboxes. Here's an example of what I have.

<imput type="checkbox" name="catID[]" value="1234">
<imput type="checkbox" name="catID[]" value="2239">
<imput type="checkbox" name="scatID[]" value="4">
<imput type="checkbox" name="scatID[]" value="34">
<imput type="checkbox" name="scatID[]" value="23">
<imput type="checkbox" name="kID[]" value="585">
<imput type="checkbox" name="concID[]" value="785">
<imput type="checkbox" name="styID[]" value="442">
<imput type="checkbox" name="styID[]" value="352">

And I need to, onSubmit check if the the user has ta least select one of the the checkboxes.
Note that they are dinamically generated so, they may be 1, 5 or more in each name (catID[], etc.)

konithomimo
12-30-2005, 03:33 AM
function validate()
{
var countChecks = 0;
var inputs = document.getElementsByTagName('input');
var i;
for(i=0;i<inputs.length;i++)
{
if((inputs[i].type == 'checkbox')&&(inputs[i].checked == true))
{
countChecks++;
}
}

if(countChecks == 0)
{
alert('Select atleast one checkbox');
}
}

MarioPro
12-30-2005, 09:52 AM
Thanks konithomimo,

Your posted function does work almost perfectly.

The only thing that was just missing in your function was a return false after the alert.


function validate(){
var countChecks = 0;
var inputs = document.getElementsByTagName('input');
var i;
for(i=0;i<inputs.length;i++){
if((inputs[i].type == 'checkbox')&&(inputs[i].checked == true)){
countChecks++;
}
}
if(countChecks == 0){
alert('Select at least one checkbox');
return false;
}
}

Thanks again for the help! :thumbsup: :thumbsup:

esthera
12-30-2005, 10:45 AM
i'm trying this but modified a drop


var countA = 0;
var inputsa = document.getElementsByTagName('sessiona');
var i;
for(i=0;i<inputsa.length;i++){
if((inputsa[i].type == 'checkbox')&&(inputsa[i].checked == true)){
countA++;
}
}

alert (countA);


I checked 2 of the checkboxes named sessiona but it is still alerting me with a 0

am i doing something wrong?

_Aerospace_Eng_
12-30-2005, 11:04 AM
There is no tag name called sessiona, the tagname is meant to be an actual html element not the name of the element.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function validate(){
var countA = 0;
var inputsa = document.getElementsByTagName('input');
var i;
for(i=0;i<inputsa.length;i++){
if((inputsa[i].type == 'checkbox')&&(inputsa[i].checked == true)&&(inputsa[i].name=='sessiona')){
countA++;
}
}
if(countA == 0){
alert('Select at least one checkbox');
return false;
}
else {
alert(countA);
return false;
}
}
</script>
</head>

<body>
<form action="#" method="post" onsubmit="return validate()">
Value 1 <input type="checkbox" name="sessiona" value="1" /><br />
Value 2 <input type="checkbox" name="sessiona" value="2" /><br />
Value 3 <input type="checkbox" name="sessiona" value="3" /><br />
Value 4 <input type="checkbox" name="sessiona" value="4" /><br />
Value 5 <input type="checkbox" name="sessiona" value="5" /><br />
Value 6 <input type="checkbox" name="sessiona" value="6" /><br />
<input type="submit" value="Submit" /><input type="reset" value="Reset" />
</form>
</body>
</html>
See if the above is what you want.

esthera
12-30-2005, 11:09 AM
not really but i got it working

by using the document.form.sessiona

insted of document.getElementsByTagName('input');

It seems to work -- is there any problem doing it that way?

Thanks for your help.

_Aerospace_Eng_
12-30-2005, 11:11 AM
That works too, no not really any problems though it could be changed to use square bracket notation. I revised my post as you were typing I guess, because it does what you want at least according to the code you posted.


document.forms[0].sessiona
where the 0 in forms[0] is the location of the form on the page from top to bottom.

MarioPro
12-30-2005, 05:19 PM
Well, just back to the post and the the first code you posted works fine with no problems, just after adding the "return false;".
It's working 100%, and believe me that sometimes the page displays a huge quantity of checkboxes.
And by the way, since I had on the <from> tag a function to disable submit, I had to add this to the input button's.



function validate(){
var countChecks = 0;
var inputs = document.getElementsByTagName('input');
var i;
for(i=0;i<inputs.length;i++){
if((inputs[i].type == 'checkbox')&&(inputs[i].checked == true)){
countChecks++;
}
}
if(countChecks == 0){
alert("To search for similarity, you have to select at least one criteria checkbox");
return false;
}
}


now my submits have


<input type="submit" name="Submit" onClick="return validate();">

Thanks for all your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum