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 regularization determine whether the input is an integer

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

Share

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

This article introduces the relevant knowledge of "how to determine whether the input is an integer or not". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Javascript regular method to judge integers: 1, open the corresponding js code file; 2, through the "var r = / ^\ +? [1-9] [0-9] * $/; String str =" 123 "; boolean flag=r.test (str);" method can be determined.

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

How does javascript regularity determine whether the input is an integer?

JS regular expression to determine whether the input is an integer or not

1. Regular expression

"^\\ integer $" / / non-negative integer (positive integer + 0) "^ [0-9] * [1-9] [0-9] * $" / / positive integer "^ (-\\ d +) | (0 +) $" / / non-positive integer (negative integer + 0) "^-[0-9] * $" / / negative integer "^ -? / / Integer "^\\ d + (\\.\\ d +)? $" / / non-negative floating point (positive floating point + 0) "^ ([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "/ / positive floating point number ^ ((-\\ d + (\\.\\ d+)?) | (0 + (\\ .0 +)) $" / / non-positive floating point number (negative floating point number + 0) "^ (- ((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 number" ^ (-?\\ d +) (\\.\\ d +)? $"/ / floating point number

2. Usage

Var r = / ^\ +? [1-9] [0-9] * $/; / / positive integer String str = "123"; boolean flag=r.test (str)

If judged to be a positive integer, flag is true

3. Addition of JS integers

First of all, make sure that all the inputs are numbers.

NText1=parseFloat (document.all.text1.value); nText2=parseFloat (document.all.text2.value); nSum=nText1+nText2 "how to determine whether the input is an integer" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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