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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:50:47+00:00 2026-06-14T06:50:47+00:00

I’ve got a Javascript function that tests a regexp that I’m intending to validate

  • 0

I’ve got a Javascript function that tests a regexp that I’m intending to validate positive decimals up to a precision of two:

function isPositiveDecimalPrecisionTwo(str) {
    return /^\d*(\.\d{1,2}$)?/.test(str);
}

I’m not getting the results I’m expecting when I call it from code.

For example:

var a = isPositiveDecimalPrecisionTwo("1");         //   expect t, returns t
var b = isPositiveDecimalPrecisionTwo("1.2");       //   expect t, returns t
var c = isPositiveDecimalPrecisionTwo("1.25");      //   expect t, returns t
var d = isPositiveDecimalPrecisionTwo("1.257");     // * expect f, returns t *
var e = isPositiveDecimalPrecisionTwo("1.2575");    // * expect f, returns t *
var f = isPositiveDecimalPrecisionTwo(".25");       // * expect t, returns f *
var g = isPositiveDecimalPrecisionTwo("d");         //   expect f, returns f
var h = isPositiveDecimalPrecisionTwo("d1");        //   expect f, returns f
var i = isPositiveDecimalPrecisionTwo("1d");        // * expect f, returns t *
var j = isPositiveDecimalPrecisionTwo("1d1");       // * expect f, returns t *
var k = isPositiveDecimalPrecisionTwo("-1");        //   expect f, returns f
var l = isPositiveDecimalPrecisionTwo("-1.5");      //   expect f, returns f

There are three issues:

  1. Validating “1.257” or “1.2575” returns true, which I though would
    return false due to the \d{1,2}
  2. Validating “.25” returns false, which I thought would return true due to the ^\d*
  3. Validating “1d” or “1d1” return true. It looks like \. is reading as any character when it looks to me like it’s a properly escaped “.” (dot).

However, when I use a tool like regexpal.com, the same regexp appears to be validating the way I expect:

http://regexpal.com/?flags=g&regex=%5E%5Cd%2B(%5C.%5Cd%7B1%2C2%7D%24)%3F&input=

What am I missing?

  • 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-14T06:50:48+00:00Added an answer on June 14, 2026 at 6:50 am

    The problem is that your $ is inside the parenthesis. You want it to be after the group.

    /^\d*(\.\d{1,2})?$/
    

    This should work the way you expect.

    With the $ inside, it was matching everything that started with a number, since the end of string ($) was “optional”.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

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.