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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to make a checkbox mandatory

    Hi I'm having a hard time making a check box mandatory, I can make text entry fields mandatory no problem but am having a hard time with a check box.

    Code:
    <form action="transact.dll" method="POST" onSubmit="return validate(form)" name="form">
    <input type="checkbox" name="checkbox" id="checkbox" value="1">&nbsp;I hereby agree to all Terms and Conditions</a>
    	<input type="submit" value="Submit">
          </form>
    
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function validate(frm) {
            if (frm.checkbox.value.checked)
        {
            alert("Please agree to our Terms and Conditons.")
            frm.checkbox.focus()
            return false
        }
    }
    //-->
    </SCRIPT>
    I know I'm missing something really simple here I just can't figure out what it is...

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Corrected code:
    Code:
    <form action="transact.dll" method="POST" onSubmit="return validate(form);" name="form">
    <input type="checkbox" name="checkbox" id="checkbox" value="1">&nbsp;I hereby agree to all Terms and Conditions</a>
    	<input type="submit" value="Submit">
          </form>
    
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function validate(frm) {
            if (frm.checkbox.checked==false)
        {
            alert("Please agree to our Terms and Conditions.");
            return false;
        }
    }
    //-->
    </SCRIPT>
    Changes are marked in red.

    - I added a few semicolons throughout just because its a bit more syntactically correct
    - checkbox.value.checked should be checkbox.checked==false. It will always return a boolean (true/false) so you can't use the if(checkbox.value.checked) method.
    - I took out the frm.checkbox.focus(); because there's really no point in focusing a checkbox. Its not like a text field where you have to click to get the cursor there, there's really not a big reason to focus it. If you want you can put that back in, though.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,639
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    Even more corrected code:
    Code:
    <script type="text/javascript">
    // <![CDATA[
    function validate(frm) {
            if (frm.checkbox.checked==false)
        {
            alert("Please agree to our Terms and Conditions.");
            return false;
        }
    }
    // ]]>
    </script>

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    @VIPStephan,

    I thought cdata were only a mandatory for XHTML for the page to validate.

    I suppose it should work well without cdata if the page was served with HTML doctype.

    And yes, language is a deprecated attribute.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,639
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    Quote Originally Posted by rangana View Post
    I thought cdata were only a mandatory for XHTML for the page to validate.
    It is but I’d say in HTML it doesn’t hurt either. In any way it’s better than the HTML comments that are commonly used.

  • #6
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys! That worked like a charm!


  •  

    Posting Permissions

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