RegEx for positive number + greater than zero an decimal (0.1)

泄露秘密 提交于 2021-01-28 15:41:20

问题


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

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