Get the App
SLTechnology News&Howtos  ›  Development  › 

How does javascript regularization determine whether the input is an integer

Shulou Source: shulou.com Published: 2022-06-03 05:21:20 09月12日 Update

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!

Tags: Integers points rules input methods content more knowledge expressions practicality learning next code usage dilemma reality situation numbers files articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL Shulou Information Huawei Docker