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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:26:20+00:00 2026-06-07T21:26:20+00:00

Possible Duplicate: Regular expression to match numbers with or without commas and decimals in

  • 0

Possible Duplicate:
Regular expression to match numbers with or without commas and decimals in text

var regex =  /^[0-9][0-9]{0,5}$|^[0-9][0-9]{0,5}[\.][0-9]$/;

valid values are
3,000
30,000
looking to have comma in the above regex . I could not get it

  • 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-07T21:26:22+00:00Added an answer on June 7, 2026 at 9:26 pm

    This regex will test for commas being in the right place:

    /^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]*)?$/
    

    You can see a working example that validates as you type here: http://jsfiddle.net/jfriend00/u9VRX/

    By way of explanation, the regex is:

    start of string
    1-3 digits
    followed by 0 or more sequences of ,nnn where n is a digit
    followed by an optional sequence that starts with a period
       and consists of 0 or more digits
    end of string
    

    If you don’t require commas and are going to parse the number anyway, then it’s easiest to just remove any commas first thus ignoring them:

    var numStr = str.replace(/,/g, "");
    

    If you want to know exactly what is wrong when it’s invalid, then that’s much more difficult to do with a pure regex. I would not find that the simplest way to solve the problem and it doesn’t generally tell you what is wrong with the number either so I would use some plain javascript first.

    function checkValid(str) {
        // check for legal characters
        if (!str.match(/^[0-9,]+(\.[0-9]*)?$/)) {
            // illegal characters present
            return("illegal_chars");
        }
    
        // strip off trailing decimal part
        var parts = str.split(".");
        if (parts.length > 2) {
            // too many periods
            return("too_many_periods");
        }
    
        // split each comma segment (if there are any)
        parts = parts[0].split(",");
        if (parts.length > 1) {
            for (var i = parts.length - 1; i > 0; i--) {
                if (parts[i].length != 3) {
                    // wrong number of digits between commas 
                    return("wrong_digits_between_commas");
                }
            }
    
            if (parts[0].length > 3) {
                // wrong number of digits in first segment before first comma
                return("too many digits before first comma");
            }
        }
    
        // if you got here without any errors, then all commas are legal
        return("");  // indicate success
    }
    

    You can see this one working as you type here: http://jsfiddle.net/jfriend00/W6FnT/

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

Sidebar

Related Questions

Possible Duplicate: How do you match only valid roman numerals with a regular expression?
Possible Duplicate: Regex to match URL Is there a regular expression to return a
Possible Duplicate: Regex to match URL I wrote a regular expression to validate only
Possible Duplicate: How to escape text for regular expression in Java Is there a
Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
Possible Duplicate: Is there a regular expression to detect a valid regular expression? Regular
Possible Duplicate: Regex to match 4 groups of letters/numbers, separated by hyphens Apologies in
Possible Duplicate: Regular expression that matches valid IPv6 addresses Can any one know the
Possible Duplicate: Regular expression: match all words except I need your help for using
Possible Duplicate: Validating an IP with regex I need the regular expression for finding

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.