Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript determine whether a specified value is a number or not

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you javascript how to judge whether the specified value is a number, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Judgment method: 1, the use of isNaN () function, syntax "isNaN (value)", if the value is a number, then return false;2, using the return value of typeof, syntax "typeof (value)", if the return value is "Number", it is a number; 3, use regular expression to judge.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript determines whether the specified value is a number.

Method 1:isNaN () function

The isNaN () function determines whether the value is nonnumeric (Not-a-Number). If the value is equal to NaN, this function returns true. Otherwise, false is returned.

Var c = "hello"; / / string isNaN (c); / / returns a true;var cantilever 10; / / numeric isNaN (c); / / returns a false

The disadvantage of isNaN () is that null, spaces, and empty strings are treated as zeros and need to be optimized.

/ * determine whether it is a number * * / function isRealNum (val) {/ / isNaN () function processes empty string spaces and NUll as 0, so remove first, if (val = = "" | val = = null) {return false } if (! isNaN (val)) {/ / for an empty array and an array with only one numeric member or a string full of numbers, / / isNaN returns false, for example: '123', [], [2], ['123'], isNaN returns false, / / so if you don't need val to include these special cases Then the judgment is rewritten to if (! isNaN (val) & & typeof val = = 'number') return true } else {return false;}}

Method 2: use the return value of typeof

Verification method: if the returned value is Number, it is a number; if the return value is String or other, it is not a number. As follows:

Var / Numbertypeof (b) / / String

Method 3: use regular expressions

(1) true is returned as long as the check is a number (including positive and negative integers, 0 and positive or negative floating point numbers)

/ * check returns true**/function isNumber (val) {var regPos = / ^\ d + (\.\ d +) as long as it is a number (including positive and negative integers, 0 and positive or negative floating point numbers)? $/ / / Nonnegative floating point number var regNeg = / ^ (- (([0-9] +\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $/ / / negative floating point if (regPos.test (val) & & regNeg.test (val)) {return true;} else {return false;}}

(2) check positive or negative numbers and return true

/ * * check a positive or negative number and return true**/function isIntNum (val) {var regPos = / ^\ dprinter; / / non-negative integer var regNeg = / ^\-[1-9] [0-9] * $/; / negative integer if (regPos.test (val) & regNeg.test (val)) {return true;} else {return false }} above is all the content of the article "how to determine whether the specified value is a number or not by javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report