SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: Checkbox

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Jakarta
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate Checkbox

    Can someone help me, I make a checkbox
    <input type="checkbox" name="Agree" value="1">
    How can I validate if the checkbox is not checked ?
    So user have to checked it first if they want to continue.
    Thanks.
    Last edited by jet_wang; Aug 20, 2001 at 04:45.

  2. #2
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you check the value to see whether it's 1 or not? ALso I think some other values are "check/unchecked" and "on/off", but I'm unsure.

    Hope this helps somewhat.
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  3. #3
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've done my checks to see whether the value for attribute "checked" is equal to true or false. For example:

    function checkIt() {
    var myVar = document.myForm.Agree.checked;
    if(myVar == true) {
    document.write("box is checked");
    }
    else {
    document.write("box is empty");
    }
    }

    I think that the true/false are booleans so they do not have to be in quotation marks but I could be wrong. Also, I work primarily with IE so I don't know how netscape will handle this.

  4. #4
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This script would be better:

    Code:
    function checkIt(loc) 
    { 
         if(loc.checked == true) 
         { 
              alert(loc + ' is checked');
         } 
         else 
         { 
              alert(loc + ' is empty');
         } 
    }
    And you'd use it like this: checkIt('myForm.myElement')
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  5. #5
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi jet_wang,

    I'm going to presume you want your validation to occur with the submission and the "cannot continue" means you want to kill the submission.

    function validate(formObj)
    {
    isOk2Send = true;
    for (i = 0; i < formObj.length; i++)
    {
    ...some validation code on checkboxes above the critical one....
    if ( (formObj.elements[i].type == 'checkbox') && (formObj.elements[i].name == 'Agree') )
    {
    if (! formObj.elements[i].checked)
    isOk2Send = false;
    }
    ...more validation if necessary....
    }
    if (isOk2Send)
    formObj.submit();
    else
    alert("You must check Agree to continue.");
    }

    <form name='something' action='something.xxx' method='something'>
    ......html....
    <input type='checkbox' name='Agree' value='1'>Agree
    <input type='button' value='submit' onClick='validate(this.form)'>
    </form>
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  6. #6
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    THe only thing I have issue with here is:

    <form name='something' action='something.xxx' method='something'>
    ......html....
    <input type='checkbox' name='Agree' value='1'>Agree
    <input type='button' value='submit' onClick='validate(this.form)'>
    </form>


    Change it to an onSubmit in the form and this will give you greater control over actual form submission, and means that if people don't hit <ENTER> the form will still get validated
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  7. #7
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jeremy,

    Either you misread something, or I am on one of my walkabouts in daze city.
    How does my example get submitted if the isOk2Send is false? I do not have a submit button in the form; the submission is through the function.
    Also, how does onSubmit give you greater control?

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  8. #8
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, it's a type="button"...

    So the user must now go to the effort of grabbing their mouse and going to the button you have specified when an onSubmit in the <form> would allow them to hit enter and still get validated information.

    How does onSubmit give you greater control, besides the above? You are staying within user norms and you don't need to do a forced JS submit.

    Also, the form still works if the user has JS turned off.
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  9. #9
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jeremy,

    phew, for a minute I thought I was having a flashback

    You are staying within user norms and you don't need to do a forced JS submit.
    Norms are norms are norms; I look at it more as programmer's choice/style.

    Also, the form still works if the user has JS turned off.
    Perhaps, but then any other javascripts on the page would not be executed either. And, in this day and age, I doubt if anyone other than a complete paranoid would have their javascript disabled.

    Peace. Different strokes for different folks.
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  10. #10
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Part of being a programmer is usability. Your ease of use is significantly decreased by requiring the users to click on a specific portion of the screen, versus allowing them to choose between doing that and hitting enter.

    Yes, the rest of the JS wouldn't execute if the user had JS turned off. However, in your version how would such a user submit the form? In mine it still gets submitted.
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  11. #11
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jeremy,

    Part of being a programmer is usability. Your ease of use is significantly decreased by requiring the users to click on a specific portion of the screen,
    from the original post:
    <input type="checkbox" name="Agree" value="1">
    The implication of the checkbox (and jet_wang's request) is that:
    1) the user must agree to continue
    2) the user has scrolled to the position where the checkbox is -- after having read x amount of text -- otherwise, there is no way the user would have any need to agree or disagree.
    3) most people use their mouse to scroll
    4) once the mouse is in hand, it is generally used to click on a checkbox
    5) most web sites do not automatically tab or place the focus on a checkbox
    6) 'significantly' ??????
    7) I personally would use buttons for agree/disagree and use them in the manner I indicated.
    8) this is getting to be more of a design argument, and costing me much more time and effort than I care to expend. jet_wang has a number of options open to him/her, perhaps more than s/he wants. Let sleeping dogs lie.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •