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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:31:56+00:00 2026-06-08T19:31:56+00:00

I want a regular expression for a textbox which allows only 3 digits and

  • 0

I want a regular expression for a textbox which allows only 3 digits and passes following criterias:

  1. Only digits (234 or 123) or
  2. Only one decimal at the end (55.1)
  3. Should not allow spaces
  4. If decimal is used then there should be a number after/before decimal as well (555. or 12. or .12 should not be allowed)

I have following RE which works partially:

/^\d{0,3}$|^\d{0,2}[\.]\d{1}$/

Any help in modifying this ?

Looks like problem is additional:

I am using the code at keypress. So it validates each pressed value at key press.

  if (window.event)    {
        knum = e.keyCode;
    }
    else if (e.which)    {
        knum = e.which;
    }
    kchar = String.fromCharCode(knum);
    numcheck = /^\d{0,3}$|^\d{0,2}[\.]\d{1}$/;

    alert(numcheck.test(kchar));

It returns false for any decimal key press. Even if I enter 55 and then try a decimal in middle to make it 5.5, ite returns false.

  • 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-08T19:31:58+00:00Added an answer on June 8, 2026 at 7:31 pm

    You need at least 1 digit, but 3 at most: \d{1,3}

    OR

    There have to be at least 1 but no more than 2 digits before and 1 after the decimal: \d{1,2}[.]\d

    So these combined: /(^\d{1,3}$|^\d{1,2}[.]\d$)/

    UPDATE:

    You are testing the character which was added on the keypress event, not the full value of the input field. This would never have the expected result.

    document.getElementById("yourfield").onkeyup = function(e) {
    
        // the event
        if (!e) e = window.event;
    
        // determine the target of the keyup event (the input field)
        var targ;
        if (e.target)
           targ = e.target;
        else if (e.srcElement)
           targ = e.srcElement;
        if (targ.nodeType == 3) // defeat Safari bug
           targ = targ.parentNode;
    
        // get the value
        kchar = targ.value;
        numcheck = /^\d{0,3}$|^\d{0,2}[\.]\d{1}$/;
    
        // test
        alert(numcheck.test(kchar));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build a regular expression which allows following cases: M1 1AA B33
I want a regular expression for decimal input, which can take 4 digits before
I want a regular expression code which allows only numeric phone number (e.g 1234567890)
I want regular expression for indian mobile numbers which consists of 10 digits. The
I want a regular expression that accepts numeric only between 15 to 17 digits.
I have a textbox and a regular expression validator applied to it. I want
I want regular expression in ruby on rails which remove all the html tags
I want a regular expression for only accepting 0-9999 No spaces, no letters. However,
i want a regular expression to validate string to have only text,operators and these
I want a regular expression that validate a string that allow only numbers 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.