Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox validation - require one or two values

Status
Not open for further replies.

vjh

Technical User
Dec 14, 2002
46
CA
Hi all,

I have a form on which I have three buttons submitting to a database. I need to validate that the checkboxes have been checked. For buttons one and two, I need to have at least one checkbox checked. For Button3, I need to have at least 2 checked.

Here's the code that submits the buttons now, without any validation:

function OnButton1()
{
document.Form1.action = "search.jsp"
document.Form1.submit();
return true;
}


function OnButton2()
{
document.Form1.action = "ill.jsp"
document.Form1.submit();
return true;
}

function OnButton3()
{
document.Form1.action = "ref.jsp"
document.Form1.submit(); return true;
}

Here's my attempt for Button1. It returns the alert, no matter what I do.

function OnButton1()
{
if (!document.Form1.elements['qq'].checked) {
alert("Please select one or more boxes!");
return false;
}
else {
document.Form1.action = "search.jsp"
document.Form1.submit();
return true;
}
return true;
}


Any suggestions to get this piece of it to work? And how do I get it to ensure there are 2 boxes checked for Button3? Can it be done with a single function called by all 3?

Thanks for any help
(-:

vj
 
Please include the html code for the form..

2b||!2b
 
var check = new Array(false, false, false);

function keepCheck(n){

check[n] = !check[n];

}

function btnOne(){

if(check[0] || check[1] || check[2]){
//code;
}
}

function btnTwo(){
if(check[0] || check[1] || check[2]){
//code;
}
}

function btnThree(){

if((check[0] && check[1]) || (check[0] && check[2]) || (check[1] && check[2])){

//code;

}
}

Then to record the checks on the check boxes put-
onclick = "keepCheck(0)"
onclick = "keepCheck(1)"
onclick = "keepCheck(2)"
 
Here's the HTML code, with some of the formatting tags and such stripped out for readability. Hopefully this won't make a difference...


<table>
<form name='Form1' method=post>

<tr><td><input type='checkbox' checked name='qq' value='116'>Poa leptocoma</td>
<td>bog bluegrass</td>

<tr><td><input type='checkbox' checked name='qq' value='170'>Poa secunda</td>
<td> Nevada bluegrass</td></tr>

</table>

<table>
<tr> <td colspan=3><b>COMPARISON OPTIONS:</b></td></tr>
<tr><!-- Add buttons -->
<td align=center>
<input type='button' value=' Describe ' name=button1 onclick='return OnButton1();'><td align=center>
<input type='button' value=' Illustrate ' name=button2 onclick='return OnButton2();'><td align=center>
<input type='button' value=' Show Differences ' name=button3 onclick='return OnButton3();'></tr>
</form>

[frog] vj
 
Maybe this example will help you:
<html>
<head>
<title></title>
<script language=&quot;JavaScript&quot;>
function vdate() {
if(document.Form1.q1.checked || document.Form1.q2.checked){
alert(&quot;you checked at least one box&quot;);}
else{ alert(&quot;please check at least one box&quot;);}
}
</script>
</head>
<body>
<table>
<form name='Form1' method=post>

<tr><td><input type='checkbox' checked name='q1' value='116'>Poa leptocoma</td>
<td>bog bluegrass</td>

<tr><td><input type='checkbox' checked name='q2' value='170'>Poa secunda</td>
<td> Nevada bluegrass</td></tr>

</table>

<table>
<tr> <td colspan=3><b>COMPARISON OPTIONS:</b></td></tr>
<tr><!-- Add buttons -->
<td align=center>
<input type='button' value=' Describe ' name=button1 onclick='return OnButton1();'><td align=center>
<input type='button' value=' Illustrate ' name=button2 onclick='return OnButton2();'><td align=center>
<input type='button' value=' Show Differences ' name=button3 onclick='return OnButton3();'></tr>
</form>
</table>
<br><a href=&quot;javascript:vdate()&quot;>test</a>
</body>
</html>

2b||!2b
 
The function then to require both:

function vdate() {
if(document.Form1.q1.checked && document.Form1.q2.checked){
alert(&quot;you checked both boxes&quot;);}
else{ alert(&quot;Please check both boxes&quot;);}
}

2b||!2b
 
Thanks for the suggestions, I'll go see if I can adapt them...

I may have misled you into thinking there were only 2 boxes. This page is generated from a search on a database. Depending on the search, there can be any number (okay between 0 and 500) of boxes in the list.

I'll let you know if I'm successful!

vj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top