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 792497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:00:08+00:00 2026-05-14T22:00:08+00:00

I was using javascript to detect for specific key strokes and while writing the

  • 0

I was using javascript to detect for specific key strokes and while writing the method I thought I’d try regular expressions and the test() method and came up with:

if (/8|9|37|38|46|47|48|49|50|51|52|53|54|55|56|57|96|97|98|99|100|101|102|103|104|105|110/.test(num)) {
    // do something if there's a match
}

This doesn’t seem to work 100% as some values seem to make it past the regex test, such as 83. I’ve since moved on, but I’m still curious as to why this didn’t work.

  • 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-14T22:00:09+00:00Added an answer on May 14, 2026 at 10:00 pm

    This is the completely wrong way to do it.

    To answer the question, the regex is matching part of your string. The string 83 passes by matching the 8.
    You need to anchor your regex by putting ^( at the beginning and )$ at the end.

    The correct way to do this is to make an array of valid numbers, and compare using parseInt.

    For example:

    var validNumbers = [ 8, 9, 37, 38, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 110 ];
    
    if (validNumbers.indexOf(parseInt(num, 10)) >=0 ) {
        //Match
    }
    

    You’ll need an indexOf function for IE:

    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(needle) {
            for(var i = 0; i < this.length; i++) {
                if(this[i] === needle) {
                    return i;
                }
            }
            return -1;
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JavaScript I'm trying to split a paragraph into it's sentences using regular expressions.
How to detect click (including browser's back button) events using javascript?
Suppose a HTML document has a iframe. Using javascript,I want to detect ,on load
Using JavaScript, is it possible to use prototype to add a method to a
Using JavaScript how do you to detect what text the user pastes into a
How to detect whether or not a particular font is installed using javascript only.
Using JavaScript, how do I NOT detect 0, but otherwise detect null or empty
How (using Javascript or jQuery) can I detect when a value has been selected
How to detect bit-version of operating system using javascript (32 or 64)? It can
Is it possible to detect, using JavaScript, when the user changes the zoom in

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.