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
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
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?