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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:43:49+00:00 2026-06-14T05:43:49+00:00

I am getting inconsistent results when using JavaScript’s RegEx to validate numbers with a

  • 0

I am getting inconsistent results when using JavaScript’s RegEx to validate numbers with a decimal place. The goal is to have any combination of digits followed by a decimal point and two more digits. It works fine except with numbers in the thousands (no separators).

This is the expression I’m using:

^[0-9]+(\.[0-9][0-9])$

Valid numbers:

10.99
0.75
999.99
5000.99
...etc

Invalid Numbers:

1000
.75
0
...etc

The problem is that it matches whole numbers in the thousands. This is for an internal application so I’m not concerned about using additional separators. I’ve tested the expression out with tools like http://regexpal.com/ which gives me the results that I need, so it appears that there is something in the JS causing the issue.

You can duplicate the problem here:
http://jsfiddle.net/hcAcQ/

  • 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-14T05:43:51+00:00Added an answer on June 14, 2026 at 5:43 am

    You need to escape the backslash before the ., I believe:

    ^[0-9]+(\\.[0-9][0-9])$
    

    The reason that a 4 digit (or greater) number will work is because the single backslash isn’t actually escaping that . to be a period character, thus causing it to act as the wildcard “match any character” dot.

    When you have 3 or fewer digits this fails because there aren’t enough characters for every match in the regex, but the with 4 digits it will work (one digit for the first character class, one for the ., and one each for the other two character classes.

    Escaping the \ will cause the . to actually be interpreted as a literal . character, as you probably intended. You could also instead define your variable as a regex literal (MDN example; near the top) so that you don’t have to deal with escaping \ characters within the string:

    //instead of new valueFormat = new RegExp('^[0-9]+(\\.[0-9])$');
    valueFormat = /^[0-9]+\.[0-9][0-9]$/;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting inconsistent results using multi-touch. I am trying to detect a 2
Using drawInRect I'm getting inconsistent results Sometimes the images draw correctly, and sometimes the
Ok so I thought it was fixed, but I'm getting totally inconsistent results. I
I am trying to parse a plain text file using Tika but getting inconsistent
I am getting some inconsistant results when using the weights argument in the coin
We've been getting inconsistent results from the Zip task in Tigris MSBuild task library,
I'm getting inconsistent results from Google maps api, |Head southwest on 吳江路/吴江路 toward 泰兴路/泰興路
I'm getting inconsistent results depending on whether I get the image directly from the
I'm getting inconsistent results with TrimEnd() under .NET 3.5. TrimEnd seems so simple to
I'm trying to vectorize a simple function in numpy and getting inconsistent behavior. I

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.