Validating CheckBoxes

Posted by Chvrsri under JavaScript on 11/3/2010 | Points: 10 | Views : 2450 | Status : [Member] [Moderator] [MVP] | Replies : 2
Hi ,
I want to validate checkboxes where and which , the user musty select atleast one check box. This validation is done after a Button is clicked. I want to fire 2 different events for single onclick event . one event at client side (which is valdiation) and other event is at server side. i mean the codebehind event.
Generally for a button we will have 2 events namely onClick() and onClientClick().

But iam usign VS2003. So i need to fire both the events . There is no onClientClick() event in VS2003.
I cannot use a button of <INPUT type=buttton> It is mandatory that i need to use only <asp:Buton>

So any help ???


Thanks,
Radha Srikanth



Responses

Posted by: Lalji_mer on: 11/3/2010 [Member] Bronze | Points: 25

Up
0
Down
try this code sample

[code]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Page</title>
<script type="text/javascript">
<!--
check = function( form ) {
try {
with( form ) { // Dual Mode
if (!( chb0.checked ) && !( chb1.checked )) {
alert( "You must atleast select 1 item in the field.");
return false;
} else {
(( chb0.checked ) ? alert( "You've selected the following item:\n" + chb0.value ) : alert( "You've selected the following item:\n" + chb1.value )); }
}
} catch( e ) {
if (!( form.chb0.checked ) && !( form.chb1.checked )) {
alert( "You must atleast select 1 item in the field.");
return false; } else {
(( form.chb0.checked ) ? alert( "You've selected the following item:\n" + form.chb0.value ) : alert( "You've selected the following item:\n" + form.chb1.value ));
}
}
}
// -->
</script>
</head>
<body>
<form id="frm" action="#" onsubmit="return check( this );">
<div>
<label for="chb0">Check Field1: <input type="checkbox" id="chb0" name="chb0" value="CheckBox #1"></label><br>
<label for="chb1">Check Field2: <input type="checkbox" id="chb1" name="chb1" value="CheckBoX #2"></label><br><br>
<input type="submit" value="submit" id="sbm" name="sbm">
</div>
</form>
</body>
</html>[/code]

more info
----------
http://www.quicksolutionproviders.com/php/javascript-validation-to-check-only-one-checkbox-among-multiple-checkboxes/

OR
select at least one checkbox and max 2 checkboxes
if( $('input[type="checkbox"]:checked').length == 2 )

{
//good
}
else
{
//bad
}

Alternatively, use
$('myForm :checkbox:checked').length


LP MER

Chvrsri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvrsri on: 11/3/2010 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down
all those methods are ok ..

But all those will work with button onClick event of <input type=submit>

But i want with <asp:button> can this be possible ???


Thanks,
Radha Srikanth

Chvrsri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response