www.webdeveloper.com
Results 1 to 2 of 2

Thread: JS - Form Validation - checkboxes

  1. #1
    Join Date
    Jun 2009
    Posts
    2

    Question JS - Form Validation - checkboxes

    I've been given this task at work and am flying blind as a non-developer so any help much appreciated. I've got a form with

    A set of 5 checkboxes (field = 'risk')
    and
    A separate checkbox (field = 'no')

    The user can select from the top options, OR select No, but if they try and pick from the five, and also No - these are mutually exclusive and an error message is needed. I've tried a few things most recently:

    function ValidateMe(f) {var maxchecked = 1;
    var count = 0;
    if(f.risk[0].checked == true) { count++; }
    if(f.risk[1].checked == true) { count++; }
    if(f.risk[2].checked == true) { count++; }
    if(f.risk[3].checked == true) { count++; }
    if(f.risk[4].checked == true) { count++; }
    if((count >= maxchecked) && (f.no.checked == true))
    {alert('Sorry, only a risk choice OR 'No' may be checked, but not both');
    return false;
    }
    return true;
    }

    and it doesn't do anything - any ideas of why, or a better way to do this?

    Thanks

  2. #2
    Join Date
    Jun 2009
    Posts
    2

    Might have cracked it -

    this seems to work:

    function ValidateMe(f) {
    if (((f.risk[0].checked == true) || (f.risk[1].checked == true) || (f.risk[2].checked == true) || (f.risk[3].checked == true) || (f.risk[4].checked == true)) && (f.no.checked == true)) {
    alert('Please select either a risk balancing option, or "No, I do not wish to accept", but NOT both');
    return false;
    }
    return true;

    I think!
    Last edited by crookedmonkey; 06-25-2009 at 11:21 AM.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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