user2320476 - 10 months ago 63

Javascript Question

This sounds easy but unable to get it to work.

I need to accept a percentage number from 0 to 100 with no decimal numbers, no negative numbers.

How can I do this validatation?

`if ((Pcomplete == "" ) || (Pcomplete < 0 && Pcomplete > 100)) {`

alert('Percentage field is required. You need to type a number between 0 and 100');

return false;

}

Answer

A Mod check will work to make sure that the variable is a whole number:

`If Pcomplete % 1 == 0`

, then it is not a decimal number.
Also, the vairbale cannot be both over 100 and less than 0, so seperate them with a ||.

```
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;
}
```