2

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;
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Baba
  • 2,059
  • 8
  • 48
  • 81

7 Answers7

5

this will always validate what you want, of course assuming it's a string when provided. Othwerise do value = "" + value or smth.

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

Only allow numbers and only numbers between 0 and -100

the regex invalidates comma and dots etc.

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
3

You should change the AND to OR, because you need the range outside.

Pcomplete < 0 || Pcomplete > 100

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

check('');
check('-1');
check('0.5');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    Nice use of '+' to convert string input to int, where string is a valid integer representation :) – GrayedFox Jun 24 '16 at 12:53
  • 1
    @GrayedFox, but not necessary here. strings became numbers with nonstrict comparisons. – Nina Scholz Jun 24 '16 at 13:29
  • True. I'm so used to using strict comparisons :) I just noticed, this answer won't catch floating point values however (would need a modulo check for that - example in accepted answer). – GrayedFox Jun 27 '16 at 08:26
  • For those interested in performance comparisons between `Math.floor` and `% 1 !== 0` using varied data sets see https://jsfiddle.net/h4kdujkk/. You'll need to be running Chrome, check the console. – GrayedFox Jun 27 '16 at 15:25
3

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;
}
kittenchops
  • 180
  • 1
  • 8
1
  • Number from 0 to 100

    if(number < 0 || number > 100 || Number.isNaN(number)) complain();
    
  • With no decimal numbers

    if(!Number.isInteger(number)) complain();
    
  • No negative numbers: already checked

If your number was a string, convert it to a number:

number = +number;

But whitespaces will be converted to 0, so before converting check

if(number.trim() === '') complain();
Oriol
  • 274,082
  • 63
  • 437
  • 513
0
(Pcomplete == "" ) || (Pcomplete < 0 && Pcomplete > 100)

The value cannot be < 0 and > 100

Pcomplete == ""  || Pcomplete < 0 || Pcomplete > 100 || Pcomplete % 1 !== 0

should evaluate

Rian O'Dwyer
  • 435
  • 2
  • 12
0
pcomplete = parseInt(pcomplete)

if (pcomplete && (pcomplete >0 && pcomplete < 100)) {

use regex

/^[1-9]{1}[0-9]{1}$/g.test("88.88")
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21
0

I found a related question, took the code from here (from user Blake Regalia): How to check if a variable is an integer in JavaScript?

if(typeof data === 'number') {
    var remainder = (data % 1);
    if(remainder === 0) {
        // yes, it is an integer
    }
    else if(isNaN(remainder)) {
        // no, data is either: NaN, Infinity, or -Infinity
    }
    else {
        // no, it is a float (still a number though)
    }
}
else {
    // no way, it is not even a number
}

Simplified version:

if(typeof data==='number' && (data%1)===0) {
    // data is an integer
}

All you need to do is integrate your range validation on either of these blocks

Community
  • 1
  • 1