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

How to determine whether it is a number in js

藏色散人
Release: 2023-01-03 09:30:14
Original
81604 people have browsed it

JS method to determine whether it is a number: 1. Use the isNaN() function to determine whether it is a number; 2. Use regular expressions to determine whether it is a number; 3. Use the return value of parseFloat() to realize the judgment.

How to determine whether it is a number in js

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Method 1: Use the isNaN() function

The isNaN() function is a global function that comes with js. The isNaN() function is used to check whether its parameter is a non-number. value.

If value x is a special non-numeric value NaN (or can be converted to such a value), the returned value is true; if value x is another value, false is returned.

The disadvantage of isNaN() is that null, space and empty string will be treated as 0

NaN: Not a Number
<script>
document.write(isNaN(123));  //数字
document.write(isNaN(-1.23));  //数字
document.write(isNaN(5-2));  //数字
document.write(isNaN(0));  //数字
document.write(isNaN("Hello"));  //字符串
document.write(isNaN("2005/12/12"));  //字符串
</script>
Copy after login

Output:

false
false
false
false
true
true
Copy after login

Disadvantage: isNaN() will treat null, Spaces and empty strings are treated as 0, so the check is not strict.

So process it and use it with the typeof operator.

Example:

// true:数值型的,false:非数值型
function myIsNaN(value) {
   document.write((typeof value === &#39;number&#39; && !isNaN(value))+"<br>");
}
myIsNaN(10); 
myIsNaN(null); 
myIsNaN( );
myIsNaN();
Copy after login

Output:

true
false
false
false
Copy after login

Method 2: Use regular expression

(1), check as long as If it is a number (including positive and negative integers, 0 and positive and negative floating point numbers), it will return true

/**
* 校验只要是数字(包含正负整数,0以及正负浮点数)就返回true
**/
function isNumber(val){
    var regPos = /^[0-9]+.?[0-9]*/; //判断是否是数字。
  
    if(regPos.test(val) ){
        return true;
    }else{
        return false;
    }
}
Copy after login

(2). If it is a positive and negative number, it will return true

/**
* 校验正负正数就返回true
**/
function isIntNum(val){
    var regPos = / ^\d+$/; // 非负整数 
    var regNeg = /^\-[1-9][0-9]*$/; // 负整数
    if(regPos.test(val) && regNeg.test(val)){
        return true;
    }else{
        return false;
    } 
}
Copy after login

Method 3 : Use the return value of parseFloat()

parseFloat() function to parse a string and return a floating point number.

This function specifies whether the first character in the string is a number. If it is, the string is parsed until it reaches the end of the number, and the number is returned as a number rather than as a string.

Usage: Parse the string specified in the parameter into a floating point number and return it.

/**
* 验证数据 是数字:返回true;不是数字:返回false
**/
function Number(val) {
  if (parseFloat(val).toString() == "NaN") {
    
    return false;
  } else {
    return true;
  }
}
//isNaN(val)不能判断空串或一个空格
//如果是一个空串、空格或null,而isNaN是做为数字0进行处理的,
//而parseInt与parseFloat是返回一个错误消息,这个isNaN检查不严密而导致的。
Copy after login

Recommended study: "js Basic Tutorial"

The above is the detailed content of How to determine whether it is a number in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
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