我尝试使用express-validator验证输入是整数还是一定范围内的浮点数。我试过了
check(
'rating',
'Rating must be a number between 0 and 5'
).isNumeric({ min: 0, max: 5 }),
但min
和max
值实际上不起作用。我试着输入大于5
的数字,它们不会抛出错误。
下面的代码可以工作,它不允许min
和max
限制之外的数字:
check(
'rating',
'Rating must be a number between 0 and 5'
).isInt({ min: 0, max: 5 })
但仅适用于整数,不适用于小数,并且输入需要是小数或0
和5
之间的整数。
有办法做到这一点吗?
5条答案
按热度按时间nwlls2ji1#
isNumeric
没有min
和max
值,您可以使用isFloat
代替:你可以在validator.js文档中阅读更多关于这些方法的内容。
7fhtutme2#
for int检查
iswrvxsc3#
isNumeric和isFloat没有最小值和最大值,你可以使用not()来代替:
但仅用于整数而不用于小数,并且输入需要是小数或1000和4000之间的整数。
谢谢大家。
6yt4nkrj4#
我使用.isInt().isNumeric()不工作,但.isFloat()为我工作
uidvcgyl5#
解决方案
它验证一般的数字,完全按照要求(“* 某个范围内的整数或浮点数 *”)