0

Hey I just need a standard JS validation to allow a string with just positive or negative #'s and no decimals.

1 = true
10 = true
-10 = true
-1.5 = false
1.5 = false

Thanks

Doc Holiday
  • 9,928
  • 32
  • 98
  • 151

1 Answers1

0

This one uses Regular Expression:

function isInteger(n) {
    return (typeof n == 'number' && /^[-]?[0-9]+$/.test(n+''));
}

This one works with strings too:

function isInteger(n) {
    return /^[-]?[0-9]+$/.test(n+'');
}

It's based on macloving's answer at How to check if a variable is an integer in JavaScript?

Fiddle

Community
  • 1
  • 1
jyrkim
  • 2,849
  • 1
  • 24
  • 33