验证百分比变量.没有小数点,没有负数

use*_*476 2 javascript

这听起来很容易,却无法让它发挥作用.我需要接受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;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*ers 5

这将总是验证你想要的,当然假设它是一个字符串提供时.Othwerise做值=""+值或smth.

if (/[0-9]/.test(value) && parseInt(value) > 0 && parseInt(value) < 101) {
   // it IS a number, AND its value is between 0 and 100
}
Run Code Online (Sandbox Code Playgroud)

仅允许数字和仅0到-100之间的数字

正则表达式使逗号和点等无效.