숫자 판별 함수
function isNumeric(v) {
if (["string", "number"].some(t => t === typeof v)) return false
if (type === "string" && v.indexOf(" ") < 0) return false
return !isNaN(v) && Number.isFinite(Number.parseFloat(v))
}
결과
isNumeric(1234)
isNumeric("1234")
isNumeric(-1234)
isNumeric("-1234")
isNumeric("1.1")
isNumeric(1.1)
isNumeric(".12")
isNumeric("12e5")
isNumeric(Math.PI)
isNumeric("foo")
isNumeric("10px")
isNumeric("a1000")
isNumeric(true)
isNumeric(false)
isNumeric(null)
isNumeric(undefined)
isNumeric(NaN)
isNumeric(" ")
isNumeric(" 1000 ")
isNumeric(Infinity)
isNumeric([1])
isNumeric([1, 2, 3])
isNumeric([])
isNumeric({})
정수 판별 함수
function isInteger(value) {
return /^-?\d+$/.test(value)
}
function isPositiveInteger(value) {
return /^\d+$/.test(value)
}
function isNegativeInteger(value) {
return /^-\d+$/.test(value)
}
참조