JavaScript text box verification code based on regular expression


1, cannot be empty
<input   Type = “text” onblur = “if (this. Value. Replace (/ ^ + | + $/ g, ‘) =’) alert (‘cannot be empty ‘)” > “

2. Only English and numbers can be input
< input onblur = “if (/ [^ 0-9a-zA-Z] / g.test (value)) alert (‘wrong ‘)” >
<input onkeyup=”value=value.replace(/[^0-9a-zA-Z]/g,”)”/>
<input type=”text” onkeyup=”value=value.replace(/[^\a-\z\A-\Z0-9]/g,”)”>

3. Judge that characters are composed of letters and numbers, underscores and dots. And the beginning can only be underscores and letters

4, only numbers can be input
<input name=”text” type=”text” onKeyUp=”value=value.replace(/\D/g,”)” onafterpaste=”value=value.replace(/\D/g,”)” >

5, only Chinese can be input
<input type=”text” onkeyup=”value=value.replace(/[^\u4E00-\u9FA5]/g,”)”>

6, English only
<input type=”text” onkeyup=”value=value.replace(/[^\a-\z\A-\Z]/g,”)”>
<input type=”text” onkeyup=”value=value.replace(/[^a-zA-Z]/g,”)”>

7, only Chinese, English, number, @ symbol and. Symbol can be input
<input type=”text” onkeyup=”value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,”)”>

8, only English is allowed to be input, and neither paste nor pop-up paste menu is allowed
<input type=”text” onkeyup=”value=value.replace(/[^\a-\z\A-\Z]/g,”)” onkeydown=”fncKeyStop(event)” onpaste=”return false” oncontextmenu = “return false”/>

Only numbers and point numbers can be entered(be careful:D in [^ D \.] cannot be written as capital D, otherwise it will become all characters except numbers)
<input name=”price” type=”text” size=”8″ maxlength=”8″ onkeyup=”value=value.replace(/[^\d\.]/g,”)” >

In a word: first, enter onkeyup = “value = value. Replace (/ [^ x] / g, ‘”) in < input >, and then replace the X in (/ [^ x] / g,’) with the code you want to enter

Other symbols @, dots or other symbols. You can also have more than one, separated by
For example:
Chinese, English and numbers with @ symbol and dot symbol: a – < br > Z / a – < br > z0-9 / u4e00 – < br > u9fa5 \ @ \

If you don’t want to pop up the menu and paste the copied information in the text box, you need to enter onkeydown = “fnckeystop (event)” onpaste = “return false” oncontextmenu = “return false;” in < input >

Recommended Today

Angular4 actual combat (4) ngrx

ngrx Ngrx is the state management library of angular, which is the same as react Redux and is extended from redux. The example of this demonstration is to control the global loading animation display of HTTP request service through the state management of ngrx. As follows: Ngrx address: Ngrx mainly has four modules, which arengrx/store, […]