Click here to Skip to main content
16,016,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Function for multiple checkbox with multiple text box input validation using javascript code.

C#
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;
        }
 
Share this answer
 
v2
Put the following Javascript in your function function ValidateForm(form)

C#
if ( form.terms.checked == false ) 
{ 
       alert ( "Please check the Conditions box." ); 
       return false; 
}
 
Share this answer
 
Write the bellow code with in validation funtion in javascript
var chkControl= document.getElementById("<%chk.ClientID%>"); 

if(!chkControl.checked )
{
alert("Required to Check!!")
}
 
Share this answer
 
Comments
bluesathish 27-Sep-13 4:18am    
if my control is a checkboxlist then you're code will works?
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:
C#
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);
       }


XML
<asp:CustomValidator ID="CustomValidator1" runat="server"
            ClientValidationFunction="validate" ErrorMessage="One of the checkboxes needs to be checked!"
            ValidationGroup=""></asp:CustomValidator>



Regards
Joachim
 
Share this answer
 
v3
Comments
rahul003 15-Jan-14 3:32am    
Hi joachim you can post the demo.
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[^]
 
Share this answer
 
Comments
bluesathish 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)



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