Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:52:24+00:00 2026-05-26T08:52:24+00:00

I am using the JavaScript to disable keyboard if checkbox is not checked and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:52:24+00:00Added an answer on May 26, 2026 at 8:52 am

    You have a bug in your code, you need double equlas == to check an equality in javascript.

    Like this:

    if(document.getElementById("check1").checked==false)

    Also, it can be very confusing to somebody else to read your code if you don’t contain all conditional stuff within a pair of curly braces, even when it’s a single line.

    So I’d refactor it ever so slightly to be like this (just for clarity, I haven’t really changed anything):

      <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;
            }
            else
            {
              return true;
            }
         }
         else
         { 
           var charCode = (evt.which) ? evt.which : event.keyCode
           if (charCode > 1 && charCode < 127)
           {
             return false;
           }
           else
           {
             return true;
           }
         }
      }
      </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to disable writing in an input field of type text using JavaScript,
Is there a way using JavaScript to disable the ability to paste text into
I need to be able to enable and disable a linkbutton using javascript as
Further to my question on disabling validators using javascript - Disable ASP.NET validators with
Can I disable right click on my web page without using JavaScript? I ask
Does anyone know how to disable an ASP.NET validator using JavaScript? I'm using javascript
using Javascript, it is easy to programatically select the text inside a textarea or
Using Javascript, I would like change the background color of the numbers in an
Possible Duplicate: how to disable javascript errors on “twebbrowser” ? I'm using the TWebBrowser
I want to disable browser refreshing using JavaScript. Currently, I am using window.onbeforeunload and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.