Regular expression for double number range validation

断了今生、忘了曾经 提交于 2020-01-03 03:01:36

问题


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


回答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



回答2:


Following regex should for you:

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

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




回答3:


How about this:

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

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




回答4:


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.




回答5:


You don't need regex at all. Some simple (and much faster than regex) math equeations will do the job:

function testNumber(number) {
    if (number < 0.1 || number > 999.9 || parseInt(10*number) != 10*number || number == parseInt(number))
        return false;

    return true;
}

alert( testNumber(12) );

In the if statement we check 4 things:

  • is the number lower than 0.1
  • is the number higher than 999.9
  • does the number have more than one digit in thefraction part
  • is the number an integer (no digits in fraction part)

If any of the conditions above is true then te function returns false.




回答6:


While you shouldn't use Regexp to compare ranges of numbers, here is one that should work for your case:

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

Example (unanchored): http://regex101.com/r/dY5pL3




回答7:


You are better off first checking if the number is a double using Regex and then checking separately if it falls inside the range. Having business logic obfuscated by something like Regex means your code is harder to maintain. And if you ever have to update your business logic to other numbers, you'll have a hard time.

[-+]?([0-9]*\.[1-9]|[0-9]+)

the above Regex validates a floating point. After that, you can just ParseFloat and compare for the rest of the logic.



来源:https://stackoverflow.com/questions/19632555/regular-expression-for-double-number-range-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!