SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Checkbox issue

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    Cleveland, TN
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox issue

    Hey all,

    My mind is drawing a blank right now. I am trying to check to make sure a user has selected a checkbox before hitting submit on a form. This is what I am trying to use:

    function doSubmit() {
    if(document.forms[0].SegDef.checked == false) {
    alert('You need to check one.');
    return false;
    }
    }

    The form is still being submitted regardless if a user makes a selection or not. I know I am missing something simple.

    Any ideas?

    Thanks.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Typically, people forget to do this:
    Code:
    <form...........onsubmit="return doSubmit()">
    Are these multiple (same-named) checkboxes? Need to see your HTML...
    ::: certified wild guess :::

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    Cleveland, TN
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes these are multiple same name checkboxes. Below is the complete function and the input field I am trying to use onClick with. Due to the way the application was written using onSubmit in the form tag is not an option.

    <input type="button" value="Next" class="buttonstyle" onClick="doSubmit();">

    function doSubmit(){
    if(document.forms[0].SegDef.Checked == false ){
    alert('You must select a segment definition');
    return false;
    }
    document.forms[0].Mode.value = 'Next';
    document.forms[0].submit();
    }

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...that's not only invaluable information, but the function you've posted is completely different. Returning false to an ordinary button does nothing, as it has no default action to cancel. Use a submit button.
    Code:
    <input type="submit" value="Next" class="buttonstyle" onclick="return fn_isOneChecked(this.form)">
    .........
    .........
    function fn_isOneChecked(oForm)
    {
    	var inputgrp = oForm.elements['SegDef'];
    	var i = inputgrp.length;
    	do
    		if (inputgrp[--i].checked)
    			return true;
    	while (i);
    	oForm.Mode.value = 'Next';
    	alert('You must select a segment definition.');
    	inputgrp[0].focus();
    	return false;
    }
    Let me know if that works, I just made it up.
    Last edited by adios; Mar 12, 2004 at 10:20.
    ::: certified wild guess :::


Bookmarks

Posting Permissions

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