Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function validate assistance

    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')

    Code:
    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;
    	}
    }

  • #2
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.)

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    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');
    }
    }

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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!
    Last edited by MarioPro; 12-30-2005 at 08:57 AM. Reason: correcting function

  • #6
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    There is no tag name called sessiona, the tagname is meant to be an actual html element not the name of the element.
    Code:
    <!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.
    Last edited by _Aerospace_Eng_; 12-30-2005 at 10:07 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.

    Code:
    document.forms[0].sessiona
    where the 0 in forms[0] is the location of the form on the page from top to bottom.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #10
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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
    Code:
    <input type="submit" name="Submit" onClick="return validate();">
    Thanks for all your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •