Home > Article > Web Front-end > JS regular expressions are limited to 1-2 digit integers or contain at most two decimal places.
【Related learning recommendations: js video tutorial】
Test code
<script type="text/javascript"> //1、只能输入数字或者小数点 仅整数,整数加小数 var reg1=/(^[0-9]{1,2}$)|(^[0-9]{1,2}[\.]{1}[0-9]{1,2}$)/; console.log(reg1.test("")+" 空串 false"); console.log(reg1.test("1")+" 1 true"); console.log(reg1.test("10")+" 10 true"); console.log(reg1.test("10.")+" 10. false"); console.log(reg1.test("100")+" 100 false"); console.log(reg1.test("100.1")+" 100.1 false"); console.log(reg1.test("10.1")+" 10.1 ture"); console.log(reg1.test("10.10")+" 10.10 true"); console.log(reg1.test("10.101")+" 10.101 false"); console.log(reg1.test("0.101")+" 0,101 false"); console.log(reg1.test("110.101")+" 110.101 false"); console.log(reg1.test("a")+" a false"); console.log(reg1.test("*")+" * false"); console.log(reg1.test("..")+" .. false"); </script>
Rendering chart
If the two-digit integer is not limited, for example, at least 1-digit integer, [0-9]{1,} can be used
The regular expression in js is "| "
//必须以数字开头和数字结尾,中间可以包含 逗号,如果只有一个数字只能是数字 var regx1=/(^[0-9]{1,}[0-9,]{0,}[0-9]{1,}$)|(^[0-9]{1}$)/;
Regular expression table matches all two-digit numbers, and the tens digit is 1
For example, I want to match all characters in the form param_tag= 12. Such an equal sign is followed by two digits, and the tens digit is 1. You can use the following regular expression to match:
parma_tag=\<1[0-9]\> ;
Regular expression: matches two digits, and the first digit cannot be 0
^[1-9][0- 9]$
js regular, you can only enter numbers with at least two significant digits, and the number of digits can be up to five (the number of digits can be adjusted by yourself)
var reg=/^[1-9]\d{0,3}\.\d$|/^[1-9]\d{0,2}\.\d{2} $|^[1-9]\d{1,4}$|^[0]\.\d{2,4}$/;
Code analysis:
var reg = /^[1-9]\d{0,3}\.\d$/ ; //首位(1-9),中间零到三位数字,接着点号,点号后一位小数(0-9) var reg = /^[1-9]\d{0,2}\.\d{2}$/; //首位(1-9),中间零到二位数字,接着点号,点号后两位小数(0-9) var reg = /^[1-9]\d{1,4}$/; //首位(1-9),中间到结尾一到四位数字,无小数 var reg = /^[0]\.\d{2,4}$/; //首位(0),接着点号,点号后二到四位小数(0-9) //合并一起写则用'|'符号相连接,即为或的意思,满足任意一种条件都算符合
js regular expression - limit number length
For example: limit the number of words to 4
var reg = /^\d{4}$ /
Explanation: It starts with four numbers and ends with these four numbers, so the number length is limited to four.
Note: These four numbers are also used
Related recommendations: Programming video course
The above is the detailed content of JS regular expressions are limited to 1-2 digit integers or contain at most two decimal places.. For more information, please follow other related articles on the PHP Chinese website!