Home > Net >  How validate digit in javascript?
How validate digit in javascript?

Time:09-27

How can i validate a input field digit value? i need to check if the user-given input is only integer or not(no float value is allowed).

var input = 10.4; //not allowed
var input = 10.0; //not allowed
var input = 10; //allowed

CodePudding user response:

As others can said you can check using isInteger, but if you want for example 10.0 disallowed you can do an extra check as such:

const input = 10.0;

if (Number.isInteger(input) || Math.trunc(input) !== input) {
  console.log('invalid')
}

CodePudding user response:

You can use like this

function isValid(num) {
  let str = num   ''
  return str.includes('.') ? 'Not Valid' : 'Valid'
}

var input = 10.4;
console.log(isValid(input))

  • Related