SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot Bruce Lee's Avatar
    Join Date
    Jan 2005
    Location
    Singapore
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validating checkbox

    I am dynamically displaying some check boxs just like the one we see in the
    mail box.

    my requirement is i should see that there should be atleast one checkbox unselected. If not stop furthur execution.

    how can i do this?

    below is the ref code.

    -----------
    DO WHILE NOT objRds.EOF
    response.write "<input type=checkbox name=movemsgid value='"
    Response.write objRds("headingid") &"' >"&strName&""
    objRds.MoveNext
    LOOP
    -------------
    onsubmit chk()
    -----------------
    function chk()
    {
    here i need to check if all the checkboxes are checked or not?
    i know some ways but then iam looking for a short way like is there any property which returns true or faslse if all checkboxs are checked or not.
    something like
    return document.myforum.checkox.selectedall
    which returns true if all the checkboxes are selected
    is there anything like that?
    }
    Happy Coding!!!
    Bruce Lee.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pass the name of your checkbox elements to this function, it returns true if 1 or more is unchecked, false if they are all checked
    Code:
    function chk(nameOfCheckbox)
    {
    	oEls = document.getElementsByName(nameOfCheckbox);
    	var i;
    	for( i = 0; i < oEls.length && oEls[i].checked; i++ );
    
    	return ( i < oEls.length );
    }

  3. #3
    SitePoint Zealot Bruce Lee's Avatar
    Join Date
    Jan 2005
    Location
    Singapore
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    working good thanks but small doubt
    say name Of Checkbox is "curName"
    when i called the function chk(curName) it didnot work
    when i called the function chk('curName') it worked
    How do i know when to use quotes and when i should not?
    Last edited by Bruce Lee; Mar 10, 2005 at 00:07.
    Happy Coding!!!
    Bruce Lee.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should use quotes if you are passing the name directly.

    And yeah, err, there is a bug
    change
    return ( i < oEls.length - 1 );
    to
    return ( i < oEls.length );

    (original post edited to remove this bug)

  5. #5
    SitePoint Zealot Bruce Lee's Avatar
    Join Date
    Jan 2005
    Location
    Singapore
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if i need to send a numeric value as 1
    then should i use chk(1) or chk('1') ?
    any restrictions to numeric values?
    Happy Coding!!!
    Bruce Lee.

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No no - you pass the name of the checkbox element.

    For example:
    <input type="checkbox" name="city" value="New York">
    <input type="checkbox" name="city" value="Los Angeles">
    <input type="checkbox" name="city" value="Boston">
    <input type="checkbox" name="city" value="Miami">
    <input type="button" onclick="chk('city');" value="Check it">

  7. #7
    SitePoint Zealot Bruce Lee's Avatar
    Join Date
    Jan 2005
    Location
    Singapore
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    checkbox prob solved.
    i mean in general if i need to send numeric value.
    then i should call add(1,2) than calling add('1','2') right?
    Happy Coding!!!
    Bruce Lee.


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
  •