Linked Questions

Popular Questions

Checkbox status changing onkeypress

Asked by At

I am using the JavaScript to disable keyboard if checkbox is not checked and to enable only numbers if checkbox is checked.

My code is.

    <script type="text/javascript">
     function isNumberKey(evt)
  {
      if(document.getElementById("check1").checked=false)
        {
        var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
     return false;
     return true;
        }
    else
        { 
         var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 1 && charCode < 127)
        return false;
            return true;
         }
  }
    </script>

HTML code

<asp:CheckBox ID="check1" runat="server"/>
<asp:TextBox ID="tbxt1" runat="server" MaxLength="6" Width="200" onkeypress="return isNumberKey(event);" TextMode="Password" autocomplete="off" />

In default as checkbox is not checked so JavaScript is not allowing me to use my keyboard. This is fine. When I change my checkbox status to checked and tried pressing a key. OnKeyPress event the checkbox status is changing to unchecked. Is there anything wrong in my code?

Related Questions