问题
I want to validate a field in javascript to have at least 1 and should be positive number or decimal.
Examples:
1
1.1
0.1
10.10
My current regex looks like this:
var _RegEx = /^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/;
回答1:
Simple:
/^\+?(\d*[1-9]\d*\.?|\d*\.\d*[1-9]\d*)$/.test(x)
Simpler:
0 < parseInt(x, 10) && parseInt(x, 10) < Infinity
Simplest:
0 < +x && +x < Infinity && !/[^\d.+]/.test(x)
Thanks to Jack, the last one is not so simple anymore. :/
回答2:
^((0?0?\.([1-9]\d*|0[1-9]\d*))|(([1-9]|0[1-9])\d*(\.\d+)?))$
12.34
00.34
0.34
109.341
0.00 (not matched)
.9
9
09
0 (not matched)
来源:https://stackoverflow.com/questions/19627995/regex-for-positive-number-greater-than-zero-an-decimal-0-1