www.webdeveloper.com
Results 1 to 11 of 11

Thread: How to validate a checkbox

  1. #1
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998

    How to validate a checkbox

    Hi Guys,

    I'm looking for the code to validate a checkbox in a form when the user submits it. Dreamweaver doesn't seem to have that capability, so... I'm looking for some magnanimous sole here to help.


    TIA
    Aronya1

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    I assume you mean checked or not:
    if(document.formname.checkboxname.checked==true) {
    alert("checked");
    }
    else {
    alert("not checked");
    }

  3. #3
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998
    Hi Fang,

    Thanks for helping, but where do I place your code?

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Between the <head> tags:

    <head>
    .
    .
    .
    <script type="text/javascript">
    <!--
    function Verify() {
    if(document.formname.checkboxname.checked==true) {
    alert("checked");
    }
    else {
    alert("not checked");
    }
    }
    //-->
    </script>
    </head>

  5. #5
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998
    It's not working.

    Does it matter that I have a typical form validation happening at the same time? (that's working fine)

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <title>Example</title>

    <style type="text/css">
    <!--
    label {display:block; margin:1em 0em}
    -->
    </style>

    <script type="text/javascript">
    <!--
    function validate(f) {
    alert(f.foo.checked ? 'checked' : 'un-checked');
    return false;
    }
    // -->
    </script>

    <form action="" onsubmit="return validate(this)">
    <div>
    <label><input type="checkbox" name="foo">Foo</label>
    <button type="submit">Submit</button>
    </div>
    </form>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  7. #7
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998
    Hi Charles,

    Thanks for wading in on this one. Your script works on its own (I haven't had time to insert it into my form yet), but it gives an alert whether the box is checked or not. I've tinkered with it, but can't get it to alert only if the box is unchecked.

    Sorry to keep coming back at you guys like this, but I don't know what I'm doing with javascript.

    Thanks again,
    Aronya1

  8. #8
    Join Date
    Mar 2003
    Location
    The Lone Star State
    Posts
    1,126
    I tried this and it works when the checkbox is unchecked.
    Originally posted by Charles
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <title>Example</title>

    <style type="text/css">
    <!--
    label {display:block; margin:1em 0em}
    -->
    </style>

    <script type="text/javascript">
    <!--
    function validate(f) {
    alert(f.foo.checked ? 'checked' : 'un-checked');
    return false;
    }
    // -->
    </script>

    <form action="" onsubmit="return validate(this)">
    <div>
    <label><input type="checkbox" name="foo">Foo</label>
    <button type="submit">Submit</button>
    </div>
    </form>
    "If at first you do suceed, try not to look astonished."
    I have 50 gmail invitations at my disposal, if you want one pm me with your First and Last name and your email address
    HTTPGuru

  9. #9
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998

    Am I missing something?

    Yes, but it gives an alert when the checkbox IS checked, too.

    I need an alert when the box is not checked. Period. Giving an alert for both states doesn't validate anything.

  10. #10
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <title>Example</title>

    <style type="text/css">
    <!--
    label {display:block; margin:1em 0em}
    -->
    </style>

    <script type="text/javascript">
    <!--
    function validate(f) {if (!f.foo.checked) {alert('Good God man! Check the box.'); f.foo.focus(); return false}}
    // -->
    </script>

    <form action="" onsubmit="return validate(this)">
    <div>
    <label><input type="checkbox" name="foo">Foo</label>
    <button type="submit">Submit</button>
    </div>
    </form>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  11. #11
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998

    Talking LOL!!!

    Charles, you're beautiful.

    Thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles