Home >Web Front-end >JS Tutorial >JavaScript determines whether the input is a numeric type
This article mainly introduces the relevant information summarized by JavaScript to determine whether the input is of numeric type. I hope this article can help everyone. Friends in need can refer to it. I hope it can help everyone.
Summary of JavaScript method to determine whether input is of numeric type
Foreword
Many times it is necessary to determine an input Whether it is a digit or not, here is a brief list of concentration methods.
The first method isNaN
isNaN Returns a Boolean value indicating whether the provided value is a reserved value NaN (not a number).
NaN is Not a Number
isNaN(numValue)
But if numValue is an empty string or a space, and isNaN is is processed for the number 0, while parseInt and parseFloat return an error message, which is caused by the loose isNaN check.
The second method is regular expression
function checkRate(input) { var re = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/ if (!re.test(nubmer)) { alert(“请输入数字”); } }
The third method uses the return value of parseFloat
function isNotANumber(inputData) { //isNaN(inputData)不能判断空串或一个空格 //如果是一个空串或是一个空格,而isNaN是做为数字0进行处理的,而parseInt与parseFloat是返回一个错误消息,这个isNaN检查不严密而导致的。 if (parseFloat(inputData).toString() == “NaN”) { //alert(“请输入数字……”); return false; } else { return true; } }
Related recommendations:
php recursive formatting number type
php Detailed explanation of the string type of numeric type
How to save and operate numeric type variables in the page in JavaScript_javascript skills
The above is the detailed content of JavaScript determines whether the input is a numeric type. For more information, please follow other related articles on the PHP Chinese website!