Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
please tell how to validate the checkbox in asp.net control using javascript validation.if the checkbox is not selected it should display an error message.
Posted 26-Feb-12 20:52pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Here's is a solution using customvalidator, not entirely javascript solution, but maybe it could be used:
 
http://www.4guysfromrolla.com/webtech/tips/t040302-1.shtml[^]
 
Or
 
Together with a custom validator and a small piece of javscript as code below:
function validate(source, arguments) {
           arguments.IsValid = false;
 
           var checklist = document.getElementById("MyCheckBoxList");
           if (checklist == null) return;
 
           var elements = checklist.getElementsByTagName("INPUT");
           if (elements == null) return;
 
           var checkBoxCount = 0;
           for (i = 0; i < elements.length; i++) {
               if (elements[i].checked) checkBoxCount++;
           }
           arguments.IsValid = (checkBoxCount > 0);
       }
 
<asp:CustomValidator ID="CustomValidator1" runat="server"
            ClientValidationFunction="validate" ErrorMessage="One of the checkboxes needs to be checked!"
            ValidationGroup=""></asp:CustomValidator>
 

Regards
Joachim
  Permalink  
v3
Comments
rahul003 at 15-Jan-14 3:32am
   
Hi joachim you can post the demo.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

 
Write the bellow code with in validation funtion in javascript
var chkControl= document.getElementById("<%chk.ClientID%>"); 
 
if(!chkControl.checked )
{
alert("Required to Check!!")
}
  Permalink  
Comments
bluesathish at 27-Sep-13 4:18am
   
if my control is a checkboxlist then you're code will works?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Put the following Javascript in your function function ValidateForm(form)
 
if ( form.terms.checked == false ) 
{ 
       alert ( "Please check the Conditions box." ); 
       return false; 
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Function for multiple checkbox with multiple text box input validation using javascript code.
 
function validateform() {
            if (document.getElementById("chkChequePicked").checked == true) {
                if (document.getElementById('txtChequePickedDate').value == "" || document.getElementById('txtChequePickedDate').value == "--Just Click--") {
                    alert('Please Enter Cheque Picked Date');
                    return false;
                }
            }
            else {
                if (document.getElementById('txtChequePickedDate').value != "") {
                    alert('Please check Cheque Picked');
                    return false;
                }
                ValidatorEnable(document.getElementById("RequiredFieldValidator4"), true);
            }
 
            if (document.getElementById("chkChequeReceived").checked == true) {
                if (document.getElementById('txtChequeReceivedDate').value == "" || document.getElementById('txtChequeReceivedDate').value == "--Just Click--") {
                    alert('Please Enter Cheque Received Date');
                    return false;
                }
            }
            else {
                if (document.getElementById('txtChequeReceivedDate').value != "") {
                    alert('Please check Cheque Received');
                    return false;
                }
                ValidatorEnable(document.getElementById("RequiredFieldValidator5"), true);
            }
 
            if (document.getElementById("chkChequeDeposit").checked == true) {
                if (document.getElementById('txtChequeDepositDate').value == "" || document.getElementById('txtChequeDepositDate').value == "--Just Click--") {
                    alert('Please Enter Cheque Deposite Date');
                    return false;
                }
            }
            else {
                if (document.getElementById('txtChequeDepositDate').value != "") {
                    alert('Please check Cheque Deposite');
                    return false;
                }
                ValidatorEnable(document.getElementById("RequiredFieldValidator6"), true);
            }
            if (document.getElementById("chkChequeCleared").checked == true) {
                if (document.getElementById('txtChequeClearedDate').value == "" || document.getElementById('txtChequeClearedDate').value == "--Just Click--") {
                    alert('Please Enter Cheque Cleared Date');
                    return false;
                }
            }
            else {
                if (document.getElementById('txtChequeClearedDate').value != "") {
                    alert('Please check Cheque Cleared');
                    return false;
                }
                ValidatorEnable(document.getElementById("RequiredFieldValidator7"), true);
            }
 
            if (document.getElementById("chkChequeRemittance").checked == true) {
                if (document.getElementById('txtChequeRemittanceDate').value == "" || document.getElementById('txtChequeRemittanceDate').value == "--Just Click--") {
                    alert('Please Enter Cheque Remittance Date');
                    return false;
                }
            }
            else {
                if (document.getElementById('txtChequeRemittanceDate').value != "") {
                    alert('Please check Cheque Remittance');
                    return false;
                }
                ValidatorEnable(document.getElementById("RequiredFieldValidator8"), true);
            }
            return true;
        }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I m sure that u have not use Google.
 
This is the third result when u type "javascript checkbox" in Google
 
http://www.netevolution.co.uk/scripts.asp?ID=25[^]
  Permalink  
Comments
bluesathish at 27-Sep-13 4:16am
   
@CRDave1988: The link you're provided was not found Mr.Dave (may be its considered to be a older post), One one more thing google result came based on several scenarios like user frequently searched tags and may vary by location you're searching. My vote 0!, oops 0 was not available ;-) Take 1!. :-)

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 576
1 OriginalGriff 183
2 Tadit Dash 150
3 Peter Leow 140
4 Sergey Alexandrovich Kryukov 121
0 Sergey Alexandrovich Kryukov 9,280
1 OriginalGriff 5,473
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 10 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid