SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    validate checkboxes w/ javascript

    Im back...Now Im trying to validate my checkboxes with javascript. Here is the code I have...

    if(document.form.C1.checked) count = count + 1
    if(document.form.C2.checked) count = count + 1
    if(document.form.C3.checked) count = count + 1
    if(document.form.C4.checked) count = count + 1
    if(document.form.C5.checked) count = count + 1
    if(document.form.C6.checked) count = count + 1
    if(document.form.C7.checked) count = count + 1
    if(document.form.C8.checked) count = count + 1
    if(document.form.C9.checked) count = count + 1
    if(document.form.C10.checked) count = count + 1
    if(document.form.C11.checked) count = count + 1
    if(document.form.C12.checked) count = count + 1
    if(document.form.C13.checked) count = count + 1
    if(document.form.C14.checked) count = count + 1
    if(document.form.C15.checked) count = count + 1
    if(document.form.C16.checked) count = count + 1
    if(document.form.C17.checked) count = count + 1
    if (count >= 1) {
    alert("Please check a box in question 5");
    form.C1.focus()
    return false; }

    it skips right over this and displays no alert box. Trying to figure out why when I leave my checkboxes empty that I dont get an error message

  2. #2
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    nevermind..after i posted my code i relieved that my >= should have been a <, gotta love the little mistakes..thanks

  3. #3
    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 williamsba,

    Thought you might like the following:

    var formObj = document.form;
    var count = 0;
    for (i=0;i<formObj.length; i++)
    {
    if(formObj.elements[i].type == 'checkbox')
    if(formObj.elements[i].checked)
    count++;
    }
    if (count < 1)
    {
    alert("Please check a box in question 5");
    form.C1.focus()
    return false;
    }

    If you do, see the "Validating Forms" script/tutorial at my site (GrassBlade) for a discussion and demonstration on validating all types of form elements.

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

  4. #4
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks for the help, but I had already figured out that my >= was supposed to be a <, thanks again...

  5. #5
    SitePoint Member doggi's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    williamsba, I think that even that you got the solution, it's worth to use Vincent's solution with a few modifications. Think about it: if your form changes, than you'll have to write another line of code. With Vincent's code the validation will still work. But I suggest to check whether at least one checkbox is checked and return from that function, so do not continue checking... Like this

    Code:
    var formObj = document.forms[0];
    var count = 0;
    for (i=0;i<formObj.length; i++)
    {
      if(formObj.elements[i].type == 'checkbox')
      {
        if(formObj.elements[i].checked)
        {
          return;
        }
      }
    }
    alert("Please check a box in question 5");
    form.c1.focus();
    This is a minor modification and it will increase performance

  6. #6
    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 doggi,

    True, it would run faster; however, I decided against the 'return' statement so as to keep the flow the same.

    Hi williamsba,

    I knew you fixed your problem. I just thought you would like to see a different approach to the same problem.

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

  7. #7
    SitePoint Member doggi's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well normally I would make an array of checkboxes and only loop through that array... That's even nicer. And you don't need the count variable anymore with a 'return'. But if the code solves it, then I wouldn't go further looking for more optimized code. But I'm not sure if the first script of williamsba would be cross browser compatible... Haven't looked at that yet.


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
  •