Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How JS can only enter integers and decimals through the regular restriction input input box

Shulou Source: shulou.com Published: 2022-06-02 04:46:09 10月02日 Update

This article mainly introduces how JS can only enter integers and decimals through the regular restriction input input box, which is very detailed and has a certain reference value. Interested friends must finish reading it!

First: the limit can only be integers

If it is not an integer, alert directly.

Second: the limit is two decimal places

Principle:

Judging by the regular expression, it does not satisfy the execution of alert.

The first regular expression is / ^ numbers / indicating that it can be one or more numbers.

The second regular expression is

/ ^ d * (?: .d {0jue 2})? $/

Indicates that it must begin with a number and end with a number.

The point here is to end the number, which is usually a decimal 1, 2 in a computer. This way of writing, which ends with a decimal point, is correct. The end of the number is forced here.

Test ()

As long as you find the satisfied part, return to the truth.

It means

/ d/. Test ('a') / / false/d/. Test ('a') / / true/d/. Test ('a') / / true

So make sure who starts and ends. Start with $and end with ^

The following is to introduce the common code that js regularities restrict the input of input boxes.

1. You can only enter numbers and English:

two。 You can only enter numbers:

3. You can only enter full-width:

4. Those who can only enter Chinese characters:

5. Email address Verification:

Var regu = "^ (([0-9a-zA-Z] +) | ([0-9a-zA-Z] + [_ .0-9akozAmurz Z -] * [0-9a-zA-Z] +) @ ([a-zA-Z0-9 -] + [a-zA-Z] {2} | net | NET | com | gov | mil | MIL | ORG | edu | EDU | int | INT) $" var re = new RegExp (regu); if (s.search (re)! =-1) {return true } else {window.alert ("Please enter a valid E-mail address!") Return false;}

6. ID card:

"^\\ d {17} (\\ d | x) $" 7.17 regular expressions "^\ dinteger $" / / non-negative integer (positive integer + 0) "^ [0-9] * [1-9] [0-9] * $" / / positive integer "^ (-\ d +) | (0 +) $" / non-positive integer (negative integer + 0) "^-[0-9] * [1-1- 9] [0-9] * $"/ / negative integer" ^ -?\\ d + $"/ / integer" ^\\ d + (\.\\ d +)? $"/ / non-negative floating point number (positive floating point number + 0) ^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *) \. [09] +) | ([09] * [1-9] [09] *) $"/ / positive floating point number" ^ ((-\\ d + (\\.\)?) | (0 + (\. 0 +)?) $"/ / non-positive floating point (negative floating point + 0)" ^ (- ([0-9] +). [09] * [1-9] [0-9] 9] *) | ([0-9] * [1-9] [0-9] *\. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $"/ / negative floating point number" ^ (-?\\ d +) (\\.\\ d +)? $"/ floating point" ^ [A-Za-z] + $"/ a 26-letter word String "^ [Amurz] + $" / / string consisting of 26 uppercase letters "^ [Amurz] + $" / / string consisting of 26 lowercase letters "^ [A-Za-z0-9] + $" / / string consisting of numbers and 26 letters "^\\ characters $" / / consisting of numbers, The 26-letter or underlined string "^ [\\ w -] + (\\ .[\\ w -] +) * @ [\\ w -] + (\\ .[\\ w -] +) + $" / / email address "^ [a-zA-z] +: / / (\\ w+ (-\\ w+) *) (\. (\\ w+ (-\\ w+) *)) * (\\?\\ S*)? $" / url

1. Cancel the dotted wireframe when the button is pressed

Add an attribute value of hideFocus or HideFocus=true to input

two。 Read-only text box content

Add attribute value readonly to input

3. Prevent backward emptying of TEXT documents (style content can be used as a class reference)

The 4.ENTER key moves the cursor to the next input box

7. Can only be numbers (no flashing)

8. You can only enter English and numbers (flashing)

9. Shielded input method

10. You can only enter numbers, decimal points, minus (-) characters (no flashing)

11. You can only enter two decimal places and three decimal places (flashing)

Tags: Input number integer decimal regular point English character limit letter string content expression address beginning decimal point attribute article legal valid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple MariaDB Shulou Information Docker