I have a set of values with the checkbox which comes from a resultset , now i need to check if anyone of the checkbox is cheked or not , if no one is checked then give a alert values , even if single checkbox is checked than do not give alert. its in asp
Very thanks to your reply , Just i am new to jquery so i didn't get this , here is my code if you can provide the function for my requirement .Thanks again
vishalvl
Member
62 Points
51 Posts
Checkbox field validation
Feb 21, 2012 05:56 PM|LINK
Hi,
I have a set of values with the checkbox which comes from a resultset , now i need to check if anyone of the checkbox is cheked or not , if no one is checked then give a alert values , even if single checkbox is checked than do not give alert. its in asp
please help.
me_ritz
Star
9337 Points
1447 Posts
Re: Checkbox field validation
Feb 21, 2012 06:05 PM|LINK
You can use jquery to achieve this easily:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script> $(document).ready(function() { $('.error').hide(); $('.submit').click(function(event){ var count=$('input:checked').length; if(count==0) { alert("select at least one"); return false; } else { return true; } }); }); </script> ------------------------------------------ <input type="checkbox" id="pizza" name="pizza" value=5 class="infobox">Pizza $5 <br> <input type="checkbox" id="hotdog" name="hotdog" value=2 class="infobox">HotDog $2<br> <input type="checkbox" id="coke" name="coke" value=1 class="infobox">Coke $1<br> <input type="checkbox" id="fries" name="fries" value=3 class="infobox">French Fries $3<br> <input class="submit" type="submit" value="Submit">vishalvl
Member
62 Points
51 Posts
Re: Checkbox field validation
Feb 21, 2012 06:09 PM|LINK
HI,
Very thanks to your reply , Just i am new to jquery so i didn't get this , here is my code if you can provide the function for my requirement .Thanks again
<h3 align="right">Add Product</h3> <form action="UpdateProduct.asp" method="POST" name="frmMain"> <p> <div class="container"> <TABLE cellspacing=0 cellpadding=3 width=525 border=0 ID="Table1"> <THEAD> <TR> <TH class="container-header" ></TH> <TH align="left" colspan=4 class="container-header"><font size="2pt" color="white" >Product Attributes</font></TH> </TR> </THEAD> <TBODY > <tr> <td></td> <td valign="top"><br> <p><STRONG>Product Name</STRONG><font color="red">*</font><br> <input type="text" name="txtProductName" size=20 maxlength=50></p> <p><STRONG>Description</STRONG><font color="red">*</font><br> <input type="text" name="txtProductDesc" size=30 maxlength=255></p> <STRONG>Vendor</STRONG><font color="red">*</font><br> <fieldset style=" border-width:0px;"> <DIV class="container" style="width:625px;"> <TABLE cellspacing=0 cellpadding=4 width=625 border=0 > <THEAD> <TR> <TH colspan=3 class="container-header">Select Vendors</TH> <TH class="container-header" style="text-align:right;"> <div class="radio"><input type="checkbox" name="checkall" id="checkall"> <label for="checkall">Check all</label></div> </TH> </TR> </THEAD> <TBODY > <% i=0 Do while not rs3.EOF If i = 0 Then i = i + 1 %> <TR> <TD nowrap ><INPUT TYPE="checkbox" VALUE="<%=rs3("VendorID")%>" id="checkbox1" name="checkbox1" ><%=rs3("Vendor")%></INPUT></TD> <% ElseIf (i < 3) Then i = i + 1 %> <TD nowrap ><INPUT TYPE="checkbox" VALUE="<%=rs3("VendorID")%>" id="checkbox1" name="checkbox1" ><%=rs3("Vendor")%></INPUT></TD> <% Else i=0 %> <TD nowrap ><INPUT TYPE="checkbox" VALUE="<%=rs3("VendorID")%>" id="checkbox1" name="checkbox1" ><%=rs3("Vendor")%></INPUT></TD></TR> <% End If%> <% rs3.movenext Loop %> </TR> </TBODY> </TABLE> </fieldset> </DIV> <p><strong>Active?</strong><br> <input type="checkbox" name="chkActive" checked></p> </td> </tr> <TBODY > </table> </div> <Div align=Center> <p> <INPUT type="button" value="Cancel" name=btnCancel onclick='location="ListProducts.asp"'> <INPUT type="submit" value="Add Product" name=btnSubmit> </Div>rajsedhain
Contributor
4181 Points
1041 Posts
Re: Checkbox field validation
Feb 21, 2012 06:15 PM|LINK
using javaScript:
var checkFound = false; for (var counter=0; counter < myForm.length; counter++) { if ((myForm.elements[counter].name == "myCheckbox") && (myForm.elements[counter].checked == true)) { checkFound = true; } } if (checkFound != true) { alert ("Please check at least one checkbox."); }try these:
http://forums.asp.net/t/1770488.aspx/1?validation+on+checkbox
http://code.google.com/p/m-jq-projects/wiki/enableControlOnCheck_jQuery
http://www.tugberkugurlu.com/archive/how-to-validate-a-checkbox-in-asp-net-3-5-checkbox-validation-control-sample-code-in-asp-net-c-sharp-c-sharp-and-visual-basic
Raj Sedhain
me_ritz
Star
9337 Points
1447 Posts
Re: Checkbox field validation
Feb 21, 2012 06:15 PM|LINK
You can just apply the script part of the previous code in the head tag of the page and it would work.
I assumed you have some button on your page..on clicking ..you want to validate your checkboxes..
paste the submit button on the page also(be sure to add class property to the button)..at least to get it work.
vishalvl
Member
62 Points
51 Posts
Re: Checkbox field validation
Feb 21, 2012 06:26 PM|LINK
Nope , it didn't worked :(
vishalvl
Member
62 Points
51 Posts
Re: Checkbox field validation
Feb 21, 2012 06:35 PM|LINK
what can be 'myForm.length' in my case.
me_ritz
Star
9337 Points
1447 Posts
Re: Checkbox field validation
Feb 21, 2012 06:42 PM|LINK
Sample code...i assumed your all checkbox id is "checkbox1" and i put it static. now paste change according to highlighted text and test. ----------------------------------------------- <html><head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script> $(document).ready(function() { $('.submit').click(function(event){ var count=$('input[id*=checkbox1]:checked').length; alert(count); if(count==0) { alert("select at least one"); return false; } else { return true; } }); }); </script> </head><body> <h3 align="right">Add Product</h3> <form action="UpdateProduct.asp" method="POST" name="frmMain"> <p> <div class="container"> <TABLE cellspacing=0 cellpadding=3 width=525 border=0 ID="Table1"> <THEAD> <TR> <TH class="container-header" ></TH> <TH align="left" colspan=4 class="container-header"><font size="2pt" color="white" >Product Attributes</font></TH> </TR> </THEAD> <TBODY > <tr> <td></td> <td valign="top"><br> <p><STRONG>Product Name</STRONG><font color="red">*</font><br> <input type="text" name="txtProductName" size=20 maxlength=50></p> <p><STRONG>Description</STRONG><font color="red">*</font><br> <input type="text" name="txtProductDesc" size=30 maxlength=255></p> <STRONG>Vendor</STRONG><font color="red">*</font><br> <fieldset style=" border-width:0px;"> <DIV class="container" style="width:625px;"> <TABLE cellspacing=0 cellpadding=4 width=625 border=0 > <THEAD> <TR> <TH colspan=3 class="container-header">Select Vendors</TH> <TH class="container-header" style="text-align:right;"> <div class="radio"><input type="checkbox" name="checkall" id="checkall"> <label for="checkall">Check all</label></div> </TH> </TR> </THEAD> <TBODY > <TR> <TD nowrap ><INPUT TYPE="checkbox" id="checkbox1" name="checkbox1" >Vendor</INPUT></TD> <TD nowrap ><INPUT TYPE="checkbox" id="checkbox1" name="checkbox1" >Vendor</INPUT></TD> <TD nowrap ><INPUT TYPE="checkbox" id="checkbox1" name="checkbox1" >Vendor</INPUT></TD></TR> </TR> </TBODY> </TABLE> </fieldset> </DIV> <p><strong>Active?</strong><br> <input type="checkbox" name="chkActive" checked></p> </td> </tr> <TBODY > </table> </div> <Div align=Center> <p> <INPUT type="button" value="Cancel" name=btnCancel> <INPUT type="submit" value="Add Product" name=btnSubmit class="submit"> </Div> </body></html>vishalvl
Member
62 Points
51 Posts
Re: Checkbox field validation
Feb 21, 2012 07:48 PM|LINK
Sorry , but still it didn't worked , i am pasting my complete asp page code , is there any problem sonewhere else?
<%@ Language=VBScript %> <!--#Include Virtual="_include/Security.asp"--> <!--#Include Virtual="_include/SelectVendors.asp"--> <% Set dbConn = Application("dbConn") Set rs3 = server.createobject("ADODB.Recordset") intProductID = Request.QueryString("ProductID") strProblem = Request.QueryString("Problem") sql3 = "Select VendorID, Vendor From ICVendors " 'response.Write sql rs3.open sql3, dbconn %> <html> <head> <meta NAME="AUTHOR" Content="David Rupe"> <link rel="stylesheet" href="/_include/styles.css"> <script SRC="/_Include/VBScript.inc" LANGUAGE="VBScript"></script> <script SRC="/_Include/CacheBuster.inc" LANGUAGE="VBScript"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script> $(document).ready(function() { $('.submit').click(function(event){ var count=$('input[id*=checkbox1]:checked').length; alert(count); if(count==0) { alert("select at least one"); return false; } else { return true; } }); }); </script> <script language="VBScript"> Sub window_onload() frmMain.txtProductName.focus End Sub Sub frmMain_onsubmit() Do Set x = window.document.frmMain If Not IsRequired(x.txtProductName,"Product Name") Then Exit Do If Not IsRequired(x.txtProductDesc,"Description") Then Exit Do If Not IsRequired(x.txtProductManu,"Manufacture") Then Exit Do Exit Sub Loop Until True window.event.returnValue = False End Sub </script> <meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </head> <body> <!--#Include Virtual="_include/start.asp"--> <h3 align="right">Add Product</h3> <form action="UpdateProduct.asp" method="POST" name="frmMain"> <p> <div class="container"> <TABLE cellspacing=0 cellpadding=3 width=525 border=0 ID="Table1"> <THEAD> <TR> <TH class="container-header" ></TH> <TH align="left" colspan=4 class="container-header"><font size="2pt" color="white" >Product Attributes</font></TH> </TR> </THEAD> <TBODY > <tr> <td></td> <td valign="top"><br> <p><STRONG>Product Name</STRONG><font color="red">*</font><br> <input type="text" name="txtProductName" size=20 maxlength=50></p> <p><STRONG>Description</STRONG><font color="red">*</font><br> <input type="text" name="txtProductDesc" size=30 maxlength=255></p> <STRONG>Vendor</STRONG><font color="red">*</font><br> <fieldset style=" border-width:0px;"> <DIV class="container" style="width:625px;"> <TABLE cellspacing=0 cellpadding=4 width=625 border=0 > <THEAD> <TR> <TH colspan=3 class="container-header">Select Vendors</TH> <TH class="container-header" style="text-align:right;"> <div class="radio"><input type="checkbox" name="checkall" id="checkall"> <label for="checkall">Check all</label></div> </TH> </TR> </THEAD> <TBODY > <% i=0 Do while not rs3.EOF If i = 0 Then i = i + 1 %> <TR> <TD nowrap ><INPUT TYPE="checkbox" VALUE="<%=rs3("VendorID")%>" id="checkbox1" name="checkbox1" ><%=rs3("Vendor")%></INPUT></TD> <% ElseIf (i < 3) Then i = i + 1 %> <TD nowrap ><INPUT TYPE="checkbox" VALUE="<%=rs3("VendorID")%>" id="checkbox1" name="checkbox1" ><%=rs3("Vendor")%></INPUT></TD> <% Else i=0 %> <TD nowrap ><INPUT TYPE="checkbox" VALUE="<%=rs3("VendorID")%>" id="checkbox1" name="checkbox1" ><%=rs3("Vendor")%></INPUT></TD></TR> <% End If%> <% rs3.movenext Loop %> </TR> </TBODY> </TABLE> </fieldset> </DIV> <p><strong>Active?</strong><br> <input type="checkbox" name="chkActive" checked></p> </td> </tr> <TBODY > </table> </div> <Div align=Center> <p> <INPUT type="button" value="Cancel" name=btnCancel onclick='location="ListProducts.asp"'> <INPUT type="submit" value="Add Product" name=btnSubmit class="submit"> </Div> </form> <!--#Include Virtual="_include/End.asp"--> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(function () { $('#checkall').click(function () { $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked); }); }); </script> </body> </html>vishalvl
Member
62 Points
51 Posts
Re: Checkbox field validation
Feb 21, 2012 09:19 PM|LINK
Thanks man , it worked :)