Home >Web Front-end >JS Tutorial >How to determine if javascript is a number

How to determine if javascript is a number

coldplay.xixi
coldplay.xixiOriginal
2021-04-02 16:29:032918browse

Javascript method to determine whether it is a number: 1. Use the [isNaN()] function, which is a global function that comes with js; 2. Use regular expressions; 3. Use the return value of [parseFloat()].

How to determine if javascript is a number

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

Javascript method to determine whether it is a number:

Method 1: Use isNaN() function

isNaN() function is The global function that comes with js, the isNaN() function is used to check whether its parameter is a non-numeric 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 processed as 0

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

Disadvantage: isNaN() will process null, space and empty string as 0, So the inspection is not rigorous.

So process it and use it with the typeof operator.

Example:

// true:数值型的,false:非数值型
function myIsNaN(value) {
   return (typeof value === &#39;number&#39; && !isNaN(value));
}
myIsNaN(10);      ----true
myIsNaN(null);    ----false
myIsNaN( );      ----false
myIsNaN();       ----false

Note: If it is '36.3', typeof value === 'number', it will return false. If you want string type numbers to also return true, you can use the following Method 3

Method 2: Use regular expression

(1) to check as long as it is a number (including positive and negative integers, 0 and positive and negative floating point numbers) It returns true

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

(2). If the positive and negative numbers are verified, it returns 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;
    } 
}

Method 3: Use the return value of parseFloat()

parseFloat() function parses a string and returns 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)Cannot judge an empty string or a space

If it is an empty string, space or null, isNaN is processed as the number 0 ,

But parseInt and parseFloat return an error message, which is caused by the loose isNaN check.

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to determine if javascript is a number. 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

Related articles

See more