0

I need to validate whether a input is a valid integer allowing for +/- entries.

I have tried this.

 function valid(elem)
 { 
      var num = parseInt(elem);
      if(!Number.isInteger(num))
      {
         alert ("Not an integer");
      }
 }

But the issue here is , it is validating even strings like 10sd as an integer. So how to validate this?

I want to validate as following:

valid(-10) = true;
valid(+10) = true;
valid(-10.01) = false;
valid(10sd) = false;
valid(10.23see) = false;
valid(10) = true;
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
AriiNkhun
  • 13
  • 5
  • 2
    Is hexadecimal, octal or binary input valid? Depending on what you consider valid, a regular expression might be the simplest approach. – Felix Kling Nov 21 '19 at 10:56
  • @NickParsons: That alone is not enough. `isNaN("5.5")` is `false`. – Felix Kling Nov 21 '19 at 10:57
  • @FelixKling oh, I see. My bad – Nick Parsons Nov 21 '19 at 10:58
  • @FelixKling just plain numbers – AriiNkhun Nov 21 '19 at 11:00
  • With "plain numbers" I assume you mean decimal numbers. Is the input a string or a number? Do you want it to accept strings? (I assumed since you use `parseInt` but I'm not sure anymore; it's also unclear whether +/- just means positive or negative numbers or whether a string can start with + or -). – Felix Kling Nov 21 '19 at 11:02
  • @FelixKling the input varies it can be string/number/decimal, i want to validate whether it is an integer. For ex. +10 and -10 is valid while 10sd or -10se aren't – AriiNkhun Nov 21 '19 at 11:05
  • You probably want `Number.isInteger(Number(elem))`, but note that a string `"0xa"` would be valid, since it's a valid hexadecimal number. Alternatively: `typeof elem !== 'number' && /^[+-]?\d+$/.test(elem) || Number.isInteger(elem)` – Felix Kling Nov 21 '19 at 11:07
  • @FelixKling your answer works great and solves my issue. Thanks for your time. Can you please put is as an answer so that i can upvote? – AriiNkhun Nov 21 '19 at 11:14
  • No, since the question is closed as duplicate ;) – Felix Kling Nov 21 '19 at 13:02

2 Answers2

0
function valid(value) {
   if(typeof value === 'number' && isFinite(value))
       alert ("It is an integer");
}

This function does the job, typeof is a keyword in Javascript which tells you the data type of the variable

You can check this out, for more usage of typeof: https://webbjocke.com/javascript-check-data-types/

Edit: made a silly error in alert, it should alert if it is an integer, the if condition checks for number

0

Simple

function valid(elem){return parseInt(elem)==elem}
Dusan Krstic
  • 663
  • 10
  • 17
  • If you already know that a value is a number (`typeof elem=="number"`) why do you use `parseInt`? `parseInt` converts strings to numbers. – Felix Kling Nov 21 '19 at 11:05
  • We need to determine if it is an integer. in fact, this is enough for this task, so the function can only be: function valid(elem){return parseInt(elem)==elem} – Dusan Krstic Nov 21 '19 at 11:06
  • Then I would suggest `Math.floor(elem) === elem` instead. Of course the whole function can be replaced with `Number.isInteger` instead. – Felix Kling Nov 21 '19 at 11:09