Home > Web Front-end > JS Tutorial > body text

JavaScript determines whether the input is a numeric type

小云云
Release: 2018-01-29 17:41:51
Original
3621 people have browsed it

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)
Copy after login

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(“请输入数字”); 
  } 
}
Copy after login

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; 
  } 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!