0

I have searched StackOverflow and I can't find an answer as to how to check for regex of numeric inputs for a calculator app that will check for the following format with every keyup (jquery key up):

  • Any integer like: 34534
  • When a dot follows the integer when the user is about to enter a decimal number like this: 34534. Note that a dot can only be entered once.
  • Any float: 34534.093485

I don't plan to use commas to separate the thousands...but I would welcome if anyone can also provide a regex for that.

Is it possible to check the above conditions with just one regex? Thanks in advance.

Kama
  • 193
  • 1
  • 3
  • 13
  • It is indeed possible. What have you tried? [This site](http://www.regular-expressions.info/javascriptexample.html) is great for quickly testing regex ideas. – chrisfrancis27 Oct 11 '12 at 22:26
  • Regex is fairly inefficient for that, This will likey work faster http://stackoverflow.com/questions/3885817/how-to-check-if-a-number-is-float-or-integer – exussum Oct 11 '12 at 22:26

5 Answers5

3

Is a lone . a successful match or not? If it is then use:

\d+(\.\d*)?|\.\d*

If not then use:

\d+(\.\d*)?|\.\d+

Rather than incorporating commas into the regexes, I recommend stripping them out first: str = str.replace(/,/g, ''). Then check against the regex.

That wouldn't verify that digits are properly grouped into groups of three, but I don't see much value in such a check. If a user types 1,024 and then decides to add a digit (1,0246), you probably shouldn't force them to move the comma.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • The problem with this (and most regex patterns you'll get for this kind of thing) is that it allows nonsensical inputs such as `,.` (for the first one) or `,.3` (for the second one) and `3,4,5` which clearly aren't well-formed. To have a regex do this properly, it gets very very messy and error-prone. Sometimes, the best thing to know about regex is when not to use them – Joe Oct 11 '12 at 22:30
  • @Joe Aye, working on more robust answers if the OP needs them. – John Kugelman Oct 11 '12 at 22:30
  • 1
    You need to anchor the regexp with `^` and `$`, otherwise it will allow non-numeric characters around the number. – Barmar Oct 11 '12 at 23:28
1

Let's write our your specifications, and develop from that.

  • Any integer: \d+
  • A comma, optionally followed by an integer: \.\d*

Combine the two and make the latter optional, and you get:

\d+\.?\d*

As for handling commas, I'd rather not go into it, as it gets very ugly very fast. You should simply strip all commas from input if you still care about them.

Zirak
  • 38,920
  • 13
  • 81
  • 92
0

you can use in this way:
[/\d+./] I think this can be used for any of your queries. Whether it's 12445 or 1244. or 12445.43

0

I'm going to throw in a potentially downvoted answer here - this is a better solution:

function valid_float (num) {
    var num = (num + '').replace(/,/g, ''), // don't care about commas, this turns `num` into a String
        float_num = parseFloat(num);
    return float_num == num || float_num + '.' == num; // allow for the decimal point, deliberately using == to ignore type as `num` is a String now
}

Any regex that does your job correctly will come with a big asterisk after it saying "probably", and if it's not spot on, it'll be an absolute pig to debug.

Sure, this answer isn't giving you the most awesomely cool one-liner that's going to make you go "Cool!", but in 6 months time when you realise it's going wrong somewhere, or you want to change it to do something slightly different, it's going to be a hell of a lot easier to see where, and to fix.

Joe
  • 15,669
  • 4
  • 48
  • 83
0

I'm using ^(\d)+(.(\d)+)+$ to capture each integer and to have an unlimited length, so long as the string begins and ends with integers and has dots between each integer group. I'm capturing the integer groups so that I can compare them.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Kevin S. Miller
  • 913
  • 1
  • 9
  • 21