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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:47:25+00:00 2026-05-28T02:47:25+00:00

I know this has been done a thousand times before but I’m having trouble

  • 0

I know this has been done a thousand times before but I’m having trouble with getting this right. I need to format the value of a text input for dollar amounts. No commas should be allowed. Only two decimal places should be allowed.
Examples of what should be allowed:

  • 100
  • 100.10
  • $100
  • $100.25

Below is my current incomplete regular expression. It currently allows multiple dollar signs to be included anywhere (not just at beginning), multiple decimal points to be included anywhere, and more than two decimal places which it shouldn’t.

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="Javascript">
    $(function(){

        jQuery('.dollarAmountOnly').keyup(function () {
            this.value = this.value.replace(/[^$0-9\.]/g, '');
        });
    });
</script>
</head>
<body>
<input type="text" class="dollarAmountOnly"/>
</body>
</html>

I would greatly appreciate help. Thanks!

  • 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-28T02:47:26+00:00Added an answer on May 28, 2026 at 2:47 am

    EDIT

    Sorry, your question clearly asked for exactly two decimal places. This is what you want:

    var r = /^\$?[0-9]+(\.[0-9][0-9])?$/;
    
    console.log(r.test("12.55"));  //true
    console.log(r.test("$12.55"));  //true
    console.log(r.test("$12"));     //true
    
    console.log(r.test("$12."));    //false
    console.log(r.test("$12.2"));    //false
    console.log(r.test("$$12"));     //false
    console.log(r.test("$12.555")); //false
    

    You have to escape your dollar sign, then you want two option digits after the decimal point:

    var r = /^\$?[0-9]+\.?[0-9]?[0-9]?$/;
    
    console.log(r.test("12.55"));  //true
    console.log(r.test("$12.55"));  //true
    console.log(r.test("$12."));    //true
    console.log(r.test("$12"));     //true
    
    console.log(r.test("$$12"));     //false
    console.log(r.test("$12.555")); //false
    

    Fiddle

    Or, to allow arbitrary digits after the decimal point, you could use a Kleene closure instead of two optional digits

    var r = /^\$?[0-9]+\.?[0-9]*$/;
    
    console.log(r.test("12.55"));  //true
    console.log(r.test("$12.55"));  //true
    console.log(r.test("$12."));    //true
    console.log(r.test("$12"));     //true
    console.log(r.test("$12.555")); //true
    
    console.log(r.test("$$12"));     //false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I know this has been asked a thousand times before, but no conclusive
I know this has been done many times before (some posts are really old
I know this has been asked at least a thousand times but I can't
I know this has been asked many times before, but i still can't seem
I don't know if this has been asked before, but we're having a discussion
I know this has been done before, but I am running into an issue
I know this question has been done but I have a slightly different twist
I know this has been asked different ways several times, but I'm just not
I know this has been asked thousands of times but I just can't find
I know this has been discussed a lot of times but is there any

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.