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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2008
    Posts
    48
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Checkbox validation

    Code:
    <html>
    <script type="text/javascript">
    function test(){
    if(document.strp.spear.checked == false){
    	alert(yes)
    }
    }
    </script>
    <form name="strp">
    <input type="checkbox" name="spear">
    <input type="button" value="Test" onclick="test()">
    </form>
    </html>
    I'm trying to learn to validate checkboxes, but it isn't working. When I run the above code, clicking the button does nothing. Any ideas?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    It does nothing because your JavaScript is invalid: You never defined the variable named yes so you get an error when you use it.

    Since you probably are not using a debugger (Why aren't you doing so? If you are writing JS code??), you don't see the error.

    Now try this code, instead:
    Code:
    <script type="text/javascript">
    function test()
    {
        alert("spear is checked? " + document.strp.spear.checked );
    }
    </script>

  • #3
    New Coder
    Join Date
    Sep 2008
    Posts
    48
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Well that's an embarassing mistake >_>. Thanks for pointing it out, and I used to use the built in IE debugger, but I'm on a different computer now, and it doesn't have IE. I'll be sure to find a different one, though.


  •  

    Posting Permissions

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