?
Solved

how to validate two seperate checkbox groups

Posted on 2012-08-28
5
Medium Priority
?
275 Views
Last Modified: 2012-08-29
Hello Experts,

I have a page that lists email recipients in 2 checkbox groups. List 1 is a static set of email addresses (simple to validate), and List 2 is created via a DB query, so the list is dynamic (some difficulty here).

My question comes in two parts.

1) Once the submit button is clicked I need to validate if ANY boxes in EITHER group has been checked. A false will generate a "you have to select a recipient" error. I just need to know the code for "if nothing from List 1 or List 2 has been selected..."

2) List 2 is dynamic. I need to know how to code javascript to be able to handle changes in List 2 to be able to work properly with the first question above.

Thank you in advance.
0
Comment
Question by:CementTruck
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:tdlewis
ID: 38342668
I've produced two helper routines so that you can easily write the code you want. For this form:
<form id=form onSubmit="return onSubmit(this)">
<input type=checkbox name="box1" value="1">One
<input type=checkbox name="box1" value="2">Two
<input type=checkbox name="box1" value="3">Three<br>
<input type=checkbox name="box2" value="a">A
<input type=checkbox name="box2" value="b">B
<input type=checkbox name="box2" value="c">C<br>
<input type=submit name="submit">
</form>
function element(theForm,name)
{
  var elem = new Array()
  for each (e in theForm.elements)
    if (e.name == name)
      elem.push(e)
  if (elem.length == 0) return null
  if (elem.length == 1) return elem[0]
  return elem
}

function isChecked(theForm,name)
{
  var isChecked = false
  for each (var box in element(theForm,name))
    isChecked |= box.checked
  return isChecked
}

function onSubmit(theForm)
{
  if (!isChecked(theForm,"box1" && !isChecked(theForm,"box2"))
  {
    alert("You must select a recipient")
    return false
  }   
  return true
}

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 38344823
Hello CementTruck,

to be able to address your second question part we need one sample html output as seen in browser html source when the page is displayed.
Can you post an example?
0
 
LVL 10

Accepted Solution

by:
tdlewis earned 2000 total points
ID: 38346322
Zvonko, the code sample I posted works regardless of how many choices are available for each checkbox.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 38346532
@CementTruck: can you post some html page source example?
0
 

Author Closing Comment

by:CementTruck
ID: 38347166
Sorry guys. Got hung up doing some other project and couldn't get back to you soon enough.

@Zvonko - I got tdlewis' code to work, and he is correct regarding the quantity of boxes. Thank you for your time.

@tdlewis - Thank you for your code.

Cheers all!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question