Regular expression for double number range validation

10,300

Solution 1

I guess this is the most accurate:

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

Note that you don't need the RegExp constructor when working with regex literals:

 re = /^(0\.[1-9]|[1-9][0-9]{0,2}(\.[1-9])?)$/
 a = [0, 0.1, 123, 123.4, '00.1', 123.45, 123456, 'foo']
 a.map(function(x) { console.log(x, re.test(x)) })

0 false
0.1 true
123 true
123.4 true
00.1 false
123.45 false
123456 false
foo false

Solution 2

Following regex should for you:

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

Live Demo: http://fiddle.re/9u9cd

Solution 3

Thought I would add an alternative way to accomplish this with a node.js javascript library that I created for generating these ranges automatically.

To use the library, you would first need to install it using npm:

$ npm install --save to-regex-range

Then add the library to your application with the following line of code

var toRegexRange = require('to-regex-range');

The main export is a function that takes two integers: the min value and max value (formatted as strings or numbers).

Examples

console.log(toRegexRange('111', '555'));
//=> 11[1-9]|1[2-9][0-9]|[2-4][0-9]{2}|5[0-4][0-9]|55[0-5]

console.log(toRegexRange('5', '5'));
//=> 5

console.log(toRegexRange('5', '6'));
//=> [5-6]

console.log(toRegexRange('51', '229'));
//=> 5[1-9]|[6-9][0-9]|1[0-9]{2}|2[0-2][0-9]

console.log(toRegexRange('29', '51'));
//=> 29|[3-4][0-9]|5[0-1]

console.log(toRegexRange('1', '100000'));
//=> [1-9]|[1-9][0-9]{1,4}|100000

The readme has more documentation and examples.

Solution 4

How about this:

var reg = new RegExp(/^\d{1,3}\.[1-9]$/);

It works with all the positive and negative cases you supplied.

Share:
10,300
Yaser Moradi
Author by

Yaser Moradi

https://github.com/bitfoundation/bitplatform https://www.linkedin.com/in/ysmoradi/

Updated on November 25, 2022

Comments

  • Yaser Moradi
    Yaser Moradi over 1 year

    I look for a regular expression to support double number with just one fraction part which ranges between 0.1 to 999.9

    It means following values are not allowed:

    0
    0.0 // less than lower bound
    0.19 // fraction part is 2 digits, right '9' is extra
    94.11 // fraction part is 2 digits, right '1' is extra
    999.90 // fraction part is 2 digits, '0' is extra
    9.0 // should be 9 or 9.1, 9.0 is wrong
    1000 // is higher than upper bound
    

    allowed ones:

    1.1
    55.5
    999.9
    

    My regular expression is:

    (^(\.[1-9])?$|(^[1-9][0-9]{0,2}?(\.[1-9])?$))$
    

    Which doesn't support 0.1 to 0.9 and extra zeros like 99.000 and 99.0

    Test steps: In your browser console:

    var reg = new RegExp(/(^(\.[1-9])?$|(^[1-9][0-9]{0,2}?(\.[1-9])?$))$/);
    reg.test(12);
    

    Any help appriciated

    • Halcyon
      Halcyon over 10 years
      reg.test("12"); because you're testing strings, not numbers.
    • matewka
      matewka over 10 years
      Do you want to test numbers or strings? Because it makes no difference in numbers how many zeros they have in the end of the fraction part. E.g. 12.1 === 12.1000.
    • Mark Reed
      Mark Reed over 10 years
      From reading the answers, there seems to be a lot of confusion around your requirements, @YasserMoradi. Perhaps you could take a step back and explain what your goal is?
    • Yaser Moradi
      Yaser Moradi over 10 years
      @MarkReed I've an validation engine that uses validations based on regular expressions and|or schematron, and tests data on both client side javaScript and server side(.NET), We are extending that, but for now, we should use this method.
    • Mark Reed
      Mark Reed over 10 years
      I get that you may have to use regexes instead of arbitrary code to do the validation, but why do you have to be so specific about the form of the number? Why is "9" OK but "9.0" bad, etc?
    • Yaser Moradi
      Yaser Moradi over 10 years
      @MarkReed I'm agree with you, but I'm responsible for the framework and validation engine itself, The business analyzers and developers are doing on their own.
    • jonschlinkert
      jonschlinkert almost 7 years
      in case it helps someone, I created a javascript tool that creates a highly optimized regex from two inputs (min/max) github.com/jonschlinkert/to-regex-range
  • Mark Reed
    Mark Reed over 10 years
    But number == parseInt(number) eliminates all integral values. Those aren't disallowed, they just need to not have a .0 tacked on. If I'm reading OP's rules right, anyway. The point is, there are restrictions on the form of the number that seem to go beyond its value and that it is a legal float.
  • matewka
    matewka over 10 years
    @MarkReed, I don't think so. Look at the 6th example in OP's disallowed numbers (9.0).
  • Mark Reed
    Mark Reed over 10 years
    Yes. But per OP, "12.1" is allowed, and "12.10000" is not.
  • Mark Reed
    Mark Reed over 10 years
    Right. The string "9.0" is disallowed. Look at comment: "should be 9"
  • matewka
    matewka over 10 years
    You're right, I didn't notice that. Now I am totally confused. The OP wants to check numbers but he doesn't assume that leading zeros doesn't matter in numbers.
  • georg
    georg over 10 years
    {1,2}? can be simply {0,2}.
  • Mark Reed
    Mark Reed over 10 years
    fractional part should be optional.
  • anubhava
    anubhava over 10 years
    @thg435: Yes that's true and that is what Teneff has posted about 13 mins later :P
  • Yaser Moradi
    Yaser Moradi over 10 years
    Your answer was helpful, but was not complete, I try to improve that. Thanks
  • Yaser Moradi
    Yaser Moradi over 10 years
    Thanks, but it fails in several tests
  • acfrancis
    acfrancis over 10 years
    @Mark Reed, I agree with you in the general case but it really depends what the asker is trying to do. I'm trying not to second-guess his requirements.
  • Yaser Moradi
    Yaser Moradi over 10 years
    Thanks for your solution, but I've to use regular expression because of limitations of our validation engine.
  • Tibos
    Tibos over 10 years
    Sorry, i had forgotten to anchor the regex (check the edited version). Let me know what tests it fails please, i couldn't find any.
  • Yaser Moradi
    Yaser Moradi over 10 years
    Very close to what we need, thank you. It doesn't support 1.80 >> false, but it's not important.
  • georg
    georg over 10 years
    @YasserMoradi: it does return false when 1.80 is given as a string: re.test("1.80"). But the numbers 1.8 and 1.80 are identical, so re.test(1.80) will be the same as re.test(1.8).
  • Yaser Moradi
    Yaser Moradi over 10 years
    Yes, I made a mistake
  • Mark Reed
    Mark Reed over 10 years
    I wasn't stating a personal belief, @acfrancis. Based on the post, integers with no fractional part ("9") are allowed, while integral values with a spurious decimal attached ("9.0") are not. See 6th example in the "incorrect" list, with associated comments.
  • Nzall
    Nzall over 10 years
    @YasserMoradi Are you sure? I used regular-expressions.info/javascriptexample.html to verify it, and the above regex gives a match for 1.11
  • Nzall
    Nzall over 10 years
    And I just realized that you want 1 decimal at most. give me a moment to fix it.
  • acfrancis
    acfrancis over 10 years
    I'm sure you're right. Reading the whole thing again, the requirements still seem odd to me. "9.0" is a good decimal number where I come from. That's a joke, btw.