Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello experts,

How to validate checkboxes on button click in asp.net c#.

I have 3 checkboxes on my page. (Note : This is not checkboxlist, These are individual checkboxes)

When i click on submit button, Message must be displayed Please select atleast one checkbox.

Thanks for your time.
Posted

C#
<script type="text/javascript">
function valid()
{
  var a=document.getElementById('<%=chek1.ClientID>');
  var b=document.getElementById('<%=chek2.ClientID>');
  var c=document.getElementById('<%=chek3.ClientID>');
if(a.checked==false && b.checked==false && c.checked==false)
{
  alert("Select atleast one");
  return false;
}
else
{
  return true;
}

}
</script>

call this script from button onclient event
C#
<asp:button id="b1" onclintclick="return valid()" xmlns:asp="#unknown"></asp:button>
 
Share this answer
 
v2
Comments
Software Engineer 892 19-Nov-14 1:42am    
Sir,

This is my code for other textboxes validation for same page.

Can you please checkboxes script also in this code. am confused.

<script type="text/javascript">
function chkAllValues()
{

if(document.getElementById('TxtFirstName').value=="" || document.getElementById('TxtFirstName').value==0)
{
alert("Please enter First Name");
document.getElementById('TxtFirstName').focus();
return false;
}

if(document.getElementById('TxtLastName').value=="" || document.getElementById('TxtLastName').value==0)
{
alert("Please enter Last Name");
document.getElementById('TxtLastName').focus();
return false;
}

if(document.getElementById('TxtDepartment').value=="" || document.getElementById('TxtDepartment').value==0)
{
alert("Please enter Department");
document.getElementById('TxtDepartment').focus();
return false;
}


return confirm('are you sure ? you want to Submit this details ?');
}



BUTTON
======
<asp:ImageButton ID="BtnSubmit" runat="server"
ImageUrl="Submit.png" CausesValidation="true"
onclick="BtnSubmit_Click" OnClientClick="return chkAllValues()"/>
</script>

Thanks in advance.
Thanks7872 19-Nov-14 2:10am    
Why confused? Confused with what?
Software Engineer 892 19-Nov-14 2:14am    
where should i place ur code. becoz we need one function/method name in OnClientClick="return chkAllValues()" like this.

Please help.
You can either validate in serverside / clientside


in Server side
check

C#
if(chb1.checked==true ||chb2.checked==true ||chb3.checked==true)
{

}
else
{
ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Check atleast one')", true);
}


in clientside
----
HTML
onclientclick="javascript:return vald();"


JavaScript
<script>
function vald(){

if($('#<%= chk1.ClientID %>').is(':checked')||$('#<%= chk2.ClientID %>').is(':checked')|\$('#<%= chk1.ClientID %>').is(':checked'))

{
return true;
}
else
{
alert(
alert('select atlest one');
return false;
}
}</script>
 
Share this answer
 
v2
C#
function chkAllValues()
{
 var a=document.getElementById('<%=chek1.ClientID>');
  var b=document.getElementById('<%=chek2.ClientID>');
  var c=document.getElementById('<%=chek3.ClientID>');
if(document.getElementById('TxtFirstName').value=="" || document.getElementById('TxtFirstName').value==0)
{
alert("Please enter First Name");
document.getElementById('TxtFirstName').focus();
return false;
}

if(document.getElementById('TxtLastName').value=="" || document.getElementById('TxtLastName').value==0)
{
alert("Please enter Last Name");
document.getElementById('TxtLastName').focus();
return false;
}

if(document.getElementById('TxtDepartment').value=="" || document.getElementById('TxtDepartment').value==0)
{
alert("Please enter Department");
document.getElementById('TxtDepartment').focus();
return false;
}

if(a.checked==false && b.checked==false && c.checked==false)
{
  alert("Select atleast one");
  return false;
}
else
{
 return confirm('are you sure ? you want to Submit this details ?');
}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900