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

  • SEARCH
  • Home
  • 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 8400817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:38:57+00:00 2026-06-09T21:38:57+00:00

I build a mechnism using JS for accepting only digits : However I want

  • 0

I build a mechnism using JS for accepting only digits :

However I want to allow :
Delete, Backspace , left arrow , right arrow :

the solution I came up with is :

assuming I write in html (which I know its bad to send event , but lets focus on the Js code please) :

<input  onKeyPress='return CheckDigit(event)'/>



function CheckDigit(evt)
{
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (((charCode >= 48) && (charCode <= 57)) || checkIfDelOrNavigate(charCode)))
        return true;
    return false;
}

  function checkIfDelOrNavigate(c)
    {
        if (((c == 46) || (c == 39) || (c == 37) || (c == 8))) return true;return false;
        /*46=delete , 39,37 = arrows , 8=backspace*/
    }

Questions :

  • Is it the right solution ? (and if it ain’t , can you please supply an answer)

  • I assume it can be changed to regex checking :

code:

 var regEx =/^[0-9]+$/;    
 isValid = regEx.test(keyChar); 
 return isValid; 

But what will be the full regEx expression ?

p.s. :
Strange but all the solutions I saw doesnt want to allow the Delete, Backspace , left arrow , right arrow chars , just the category itself.

  • 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-06-09T21:38:58+00:00Added an answer on June 9, 2026 at 9:38 pm

    I combine the answer to this question with the following check to make sure I only capture digits while allowing for non-printable characters. You’ll have to do something about the paste operation, though.

    return !window.isNaN(String.fromCharCode(event.which))
    

    See jsFiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that I want to build using automake. The project consists
Build a menu using the WPF Ribbon. My application is in the MVVM pattern.
I use teamcity to build Sharepoint projects using 64-bit MSBuild. All projects work fine
I am attempting to build an authentication mechanism using API keys for access. As
We are using SCons for all our build need, and we would like to
I need to build Android application using Java which is able to send request
I am having a java project with a ant build file, using this ant
I'm using the Textpattern CMS to build a discussion site-- I have a firm
I'm using only CCRC - I don't have ClearCase locally installed. I use CCRC
I would like to build thick client using web browser, Silverlight and some database

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.