Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Can any one please tell me how to validate a checkbox using javascript. Actually, my requirement is: the checkbox is to be checked only when a particular text box has been given a value. Until, the textbox is not filled, this checkbox is not allowed to be checked. Please tell me how can I implement this in my asp.net page.

Thanks in advance
Posted
Comments
Ankur\m/ 26-May-10 0:40am    
You haven't mentioned if your problem was solved. If an answer, answers your question, accept that answer by selecting "Accept Answer".

Hi,

Call this javascript function onClientClick of TextBox-

C#
function DoTheCheck() {
if(document.myform.box1.checked == false)
{ document.myform.box1.checked = true;
; }



Regards,

Nilesh Shah
 
Share this answer
 
You'd need to break this down into a couple of steps

Your checkbox initial state should be disabled so the user can't simply click on it

http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.html[^]

You then want to hook into the textbox 'onchange' event a have a function that validates the entry of the text box, making sure it's length > 0 or whatever your requirements are.

If it's OK, then enable the relevant checkbox and set it to checked.

In jQuery, this might read like

C#
$(document).ready(function() {
    //JQuery keyup event
    $("#myTextBox").keyup(function() {
        
        var isValid = $(this).val().length > 0;
        $('#myCheckBoxItem').attr('checked', isValid )
 
    });
});
 
Share this answer
 
v2
Create texbox
XML
<asp:TextBox ID="TextBox1" runat="server" onblur="javascript:validate();"></asp:TextBox>

javascript
JavaScript
function validate()
{
    if(document.getElementById("TextBox1").value == checkValue)
    {
        document.getElementById("CheckBox1").checked = true;
    }
}

where checkValue is the value you want to check.

Hope this helps!
 
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