0

I want to write a regex that allows an integer number, or a decimal number with 0 - 2 decimal digits.

Valid Input

1
1.
1.1
1.11
111111111
111111111.
111111111.1
111111111.11

Invalid Input

a
a.
1.a
1.111
1.1111
  • string allows any number of digit characters, but only allows 1 decimal/period
  • if a period/decimal exists: only allow 2 digit characters after the decimal

Here is the regex I came up with

\d*\.?([\d]){0,2}

I am having trouble testing it to ensure it works. I found a couple ways of testing it. Using the test method which I just used w3schools setup here. The other way was with some javascript regular expression tester like regexr and regex101. For all of these: it appears that either my regular expression is returning false positives, or my regular expression does not work as intended.

Question: What regex would do what I want it to do?

1
  • That regex should be fine, though perhaps you want to include the start/end of line rules to: ^\d*\.?([\d]){0,2}$ Commented Nov 4, 2016 at 13:55

2 Answers 2

5
  1. You need to make sure that you check the complete string (from the first char to the last char) using ^...$
  2. The first digit should appear at least 1 time (so you want to use + and not *).

Check this example:

r = /^\d+\.?\d{0,2}$/
tests = ['1', '1.', '1.1', '1.11', '111111111', '111111111.', '111111111.1', '111111111.11', 'a', 'a.', '1.a', '1.111', '1.1111']
tests.forEach(function(val) {
  console.log(val, val.match(r) ? ': valid' : ': invalid');
});

update

Following the comments - if you need a solution for "integer number, or a decimal number with 0 - 2 decimal digits" (like you said in the question, but not like the valid input section), you can use this:

r = /^\d+(\.\d\d{0,1})?$/
console.log('1.'.match(r))

tests = ['1', '1.', '1.1', '1.11', '111111111', '111111111.', '111111111.1', '111111111.11', 'a', 'a.', '1.a', '1.111', '1.1111']
tests.forEach(function(val) {
  console.log(val, val.match(r) ? ': valid' : ': invalid');
});

Sign up to request clarification or add additional context in comments.

10 Comments

@RoryMcCrossan, I think it should be a + and not * on the first \d.
I already deleted my answer and upvoted yours, you beat me to it :)
@DonCallisto, what is the problem with 12.? The OP said it's a valid input... (if it wasn't valid I would change the regex to set it as invalid...)
@Dekel where he said that?
@Neil, w3schools don't save your code. You can use jsfiddle.net for that
|
0

Don't forget closing and opening in Regex, many new Regex for get it and they end up unwanted result, everything should between ^ and $, ^ is starting point of the word(digit) boundary and $ is ending point...something like below should help you, try:

'use strict';
var decimal = /^\d+(\.\d\d{0,2})$/, num = 111111111.11;
console.log(decimal.test(num));

Hope this helps...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.