SoFunction
Updated on 2025-03-10

JS can only enter integers, decimals (amount or cash) and two decimals through regular restrictions.

First: The limit can only be integers

<input type = "text" name= "number" id = 'number' onkeyup= "if(! /^d+$/.test()){alert('Only integers');='';}" />

If it is not an integer, just alert

Second: The limit is a decimal number of two digits

<input type = "text" name= "price" id = 'price' onkeyup= "if( ! /^d*(?:.d{0,2})?$/.test()){alert('Only enter numbers, only two digits can be retained after the decimal point');='';}" />

principle:

Judging by regular expressions, the execution of alert is not satisfied.

The first regular expression is /^d+$/ which can be one or more numbers

The second regular expression is

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

It must be the beginning of a number and the end of a number.

The key here is to end numbers. In computers, decimals usually end with 1., 2. This way of writing is to end with decimal points, which is correct. Force the end of the numbers here.

test()

As long as the satisfied part is found, return to true.

Meaning yes

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

So we must ensure who starts and ends. Use $ at the beginning, and use ^ at the end

Below is a common code for inputting js regular restriction input box

1. Only enter numbers and English:

<input onkeyup="value=(/[\W]/g,'') " 
onbeforepaste="('text',('text').replace(/[^\d]/g,''))" 
ID="Text1" NAME="Text1"> 

2. Only numbers can be entered:

<input onkeyup="value=(/[^\d]/g,'') " 
onbeforepaste="('text',('text').replace(/[^\d]/g,''))" 
ID="Text2" NAME="Text2">

3. Only input full-width:

<input onkeyup="value=(/[^\uFF00-\uFFFF]/g,'')" 
onbeforepaste="('text',('text').replace(/[^\uFF00-\uFFFF]/g,''))" 
ID="Text3" NAME="Text3"> 

4. Only Chinese characters can be entered:

<input onkeyup="value=(/[^\u4E00-\u9FA5]/g,'')" 
onbeforepaste="('text',('text').replace(/[^\u4E00-\u9FA5]/g,''))" 
ID="Text4" NAME="Text4"> 

5. Email address verification:

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

6. Identity card:

"^\\d{17}(\\d|x)$"
 7.17 regular expressions
 "^\\d+$"//Non-negative integer (positive integer + 0)"^[0-9]*[1-9][0-9]*$"//Positive integer"^((-\\d+)|(0+))$"//Not positive integer (negative integer + 0)"^-[0-9]*[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]*\\.[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"^[A-Za-z]+$"//A string composed of 26 English letters"^[A-Z]+$"//A string composed of 26 English letters capitalization"^[a-z]+$"//A string composed of lowercase of 26 English letters"^[A-Za-z0-9]+$"//A string composed of numbers and 26 English letters"^\\w+$"//A string composed of numbers, 26 English letters or underscores"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"//email address"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"//url 

1. The dotted box when the cancel button is pressed

Add attribute value hideFocus or HideFocus=true in input

2. Read-only text box content

Add attribute value in input readonly

3. Prevent TEXT documents from being cleared after retreat (the style content can be used as a class reference)

 <INPUT style=behavior:url(#default#savehistory); type=text 
id=oPersistInput> 

The key moves the cursor to the next input box

 <input onkeydown="if(==13)=9" > 

5. Only in Chinese (with flashing)

 <input onkeyup="value="/(/[" -~]/g,'')" 
onkeydown="if(==13)=9"> 

6. Only numbers (with flashes)

<input onkeyup="value="/(/["^\d]/g,'') 
"onbeforepaste="('text',('text').replace(/[^\d]/g,''))"> 

7. Only numbers (no flashing)

<input ime-mode:disabled" 
onkeydown="if(==13)=9" onKeypress="if 
((<48 || >57)) =false"> 

8. Only enter English and numbers (with flashing)

<input onkeyup="value="/(/[\W]/g,"'')" 
onbeforepaste="('text',('text').replace(/[^\d]/g,''))"> 

9. Blocking input method

<input type="text" name="url" ime-mode:disabled" 
onkeydown="if(==13)=9"> 

10. Only enter numbers, decimal points, minus signs (-) characters (no flash)

 <input onKeyPress="if (!=46 && !=45 && 
(<48 || >57)) =false"> 

11. Only two decimal places and three decimal places (with flash)

<input maxlength=9 
onkeyup="if((/^\d{3}$/))value="/(value,parseInt(value/10))" 
;value="/(/\.\d*\./g,'."')" onKeyPress="if((<48 
|| >57) && !=46 && !=45 || 
(/^\d{3}$/) || /\.\d{3}$/.test(value)) 
{=false}" id=text_kfxe name=text_kfxe>