Click here to Skip to main content
Sign Up to vote bad
good
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:52


5 solutions

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  
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  
 
Write the bellow code with in validation funtion in javascript
var chkControl= document.getElementById("<%chk.ClientID%>"); 
 
if(!chkControl.checked )
{
alert("Required to Check!!")
}
  Permalink  
Put the following Javascript in your function function ValidateForm(form)
 
if ( form.terms.checked == false ) 
{ 
       alert ( "Please check the Conditions box." ); 
       return false; 
}
  Permalink  
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  

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 Sergey Alexandrovich Kryukov 435
1 OriginalGriff 315
2 Arun Vasu 293
3 CPallini 213
4 Zoltán Zörgő 194
0 Sergey Alexandrovich Kryukov 10,105
1 OriginalGriff 7,739
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 2,999


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