Explanation
. 1 the onkeyup "= button trigger lifted," onafterpaste "= trigger after the paste, means for limiting the overall 2 . 3 \ D: Similar [^ 0-9 ] non-numeric . 4 / G: Global . 5 the this .Value = the this . value.replace (/ \ D / G, '' ): the numbers of all non-empty replaced . 6 . 7 IMG is a schematic for an optional parameter . 8 I ignore case . 9 m multiline mode 10 G global schema . 11 12 is \ D is equivalent to [^ 0-9 ] 13 is \ W is equivalent to [A-Za-Z ~ 0-9 ] 14 \ W is equivalent to [^ A-Za-Z ~ 0-9 ] 15 16 ? match length equivalent to {0 , 1 } 17 * match length equivalent to {0 ,} 18+ Is equivalent to the length of the match. 1 { ,} . 19 \ D equivalent to [0-9]
html:
. 1 <! DOCTYPE HTML > 2 < HTML lang = "EN" > . 3 < head > . 4 < Meta charset = "UTF-. 8" > . 5 < title > the Title </ title > . 6 </ head > . 7 < body > . 8 . 9 can only enter a positive integer (a decimal point can not be entered): < iNPUT type = "text" placeholder = "enter numbers only" the onkeyup = "this.value this.value.replace = (/ \ D / G,'')" 10 onafterpaste = "this.value = this.value.replace (/ \ D / G, '')" > < br > . 11 only letters: < INPUT type = "text" placeholder = "only letters" the onkeyup = "value.replace value = (/ [A-Za-Z ^] / IG, '')" > < br > 12 is only input Chinese characters: < iNPUT type = "text" placeholder = "Chinese character input only" the onkeyup = "value.replace value = (/ [^ [\ u4e00- \ u9fa5] * / IG, '')" > < br > 13 is 14 only numbers and letters (not enter symbolic): <input type="text" placeholder= "Enter only numbers and letters" the onkeyup = "value = value.replace (/ [^ A-Za-Z0-9] / IG, '')" > < br > 15 can only enter numbers and characters: < INPUT type = "text" placeholder = "only enter numbers and characters" the onkeyup = "value = value.replace (/ [^ 0-9 \ u4e00- \ u9fa5] / G, '')" > < br > 16 only enter letters and characters: < iNPUT type = "text" placeholder = "only letters and characters" the onkeyup = "value = value.replace (/ [A-Za-Z ^ \ u4e00- \ u9fa5] / G, '' ) " > < br > . 17 18 is only input nonzero positive integer: < INPUT type = "text"placeholder = "Enter only nonzero positive integer" . 19 the onkeyup = "IF (this.value.length ==. 1) = {this.value this.value.replace (/ [^ 1-9] / G, '' )} {this.value = this.value.replace the else (/ \ D / G, '')} " > < br > 20 is </ body > 21 is < Script > 22 is // only input is greater than or equal to zero or decimal 23 is function IsIntegerOrDecimal (ID) { 24 var STR = document.getElementById (ID) .value.trim (); 25 IF (str.length ! = 0 ) { 26 is var REG = /^[1-9]\d*(\.[0-9]+)?|0(\.[0-9]+)?$/; 27 if (!reg.test(str)) { 28 return false; 29 } else { 30 return true; 31 } 32 } 33 } 34 </script> 35 </html>