Home >Web Front-end >JS Tutorial >A summary of how JavaScript determines that the input is a numeric type

A summary of how JavaScript determines that the input is a numeric type

黄舟
黄舟Original
2017-09-28 10:20:231309browse

This article mainly introduces the relevant information summarized by JavaScript to determine whether the input is of a numeric type. I hope this article can help everyone. Friends in need can refer to

JavaScript to determine whether the input is of a numeric type. Summary of methods for numeric types

Preface

Many times it is necessary to determine whether an input is a digit. Here is a brief list of concentrated 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; 
  } 
}

The above is the detailed content of A summary of how JavaScript determines that the input is a numeric type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn