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 verify that it is a numeric type

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces javascript how to verify whether it is a digital type, the article is very detailed, has a certain reference value, interested friends must read it!

The method of javascript verifying whether it is a numerical type: 1, using isNaN () function to judge whether the value is a number; 2, using regular expression to judge; 3, using parseFloat () function to judge.

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

Is javascript verification a numeric type? JS determines whether the value is a number.

1. Use the isNaN () function

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

NaN: Not a Number

/ * 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;}}

The most correct way to determine whether a value is a number by the isNaN () function is:

/ / true: numeric, false: non-numeric function myIsNaN (value) {return typeof value = = 'number' & &! isNaN (value);}

Detailed explanation of isNaN ()

Reference link: https://wangdoc.com/javascript/types/number.html#isnan

IsNaN returns false for empty arrays and arrays with only one numeric member.

IsNaN ([]) / / falseisNaN ([123]) / / falseisNaN (['123']) / / false

The above code returns false because these arrays can be converted to numeric values by the Number function, see the "data type conversion" chapter.

Therefore, it is best to determine the data type before using isNaN.

Function myIsNaN (value) {return typeof value = = 'number' & &! isNaN (value);}

two。 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;}}

3. Use the parseFloat () function

/ * verify that the data is a number: return true Not a number: returns false**/function Number (val) {if (parseFloat (val). ToString () = "NaN") {return false;} else {return true }} / / isNaN (val) cannot determine an empty string or a space / / if it is an empty string, space, or null, and isNaN is processed as the number 0, while parseInt and parseFloat return an error message, which is caused by a lack of isNaN checking. The above is all the content of the article "how javascript verifies whether it is a digital type". Thank you for reading! Hope to share the content to help you, more related 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: 210

*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