gpt4 book ai didi

javascript - 验证百分比变量。无小数点,无负数

转载 作者:行者123 更新时间:2023-12-03 23:46:42 24 4
gpt4 key购买 nike

这听起来很简单,但无法使其正常工作。
我需要接受一个从0到100的百分比数字,没有小数,没有负数。
我该如何进行验证?

if ((Pcomplete == "" ) || (Pcomplete < 0 && Pcomplete > 100)) {
alert('Percentage field is required. You need to type a number between 0 and 100');
return false;
}

最佳答案

Mod检查将确保该变量是整数:
If Pcomplete % 1 == 0,那么它不是十进制数字。
另外,副词不能同时大于100和小于0,因此请用||分隔。

if ((Pcomplete == "" ) || (Pcomplete < 0) || (Pcomplete > 100) || (PComplete % 1 !==0) {
alert('Percentage field is required. You need to type a whole number between 0 and 100');
return false;
}

关于javascript - 验证百分比变量。无小数点,无负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38013757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com