Sometimes you need to limit the type of text box input content, this section share the regular expression limit text box can only enter numbers, decimal points, English letters, Chinese characters and other code.
For example, enter a positive integer greater than 0
Copy CodeThe code is as follows:
<input onkeyup= "if (this.value.length==1) {this.value=this.value.replace (/[^1-9]/g, ')}else{this.value= This.value.replace (/\d/g, ')} "onafterpaste=" if (this.value.length==1) {this.value=this.value.replace (/[^1-9]/g, ' ')}else{this.value=this.value.replace (/\d/g, ')} ">
1, the text box can only enter a numeric code (the decimal point can not be entered)
Copy CodeThe code is as follows:
<input onkeyup= "This.value=this.value.replace (/\d/g, ')" onafterpaste= "This.value=this.value.replace (/\D/g, ' ) ">
2, only the number can be entered, the decimal point will be lost.
Copy CodeThe code is as follows:
<input onkeyup= "if (IsNaN (value)) ExecCommand (' Undo ')" Onafterpaste= "if (IsNaN (value)) ExecCommand (' Undo ')" >
<input name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' Input number only '); this.value= ';}" >
3, digit and decimal method two
Copy CodeThe code is as follows:
<input type=text t_value= "" o_value= "" onkeypress= "if (!this.value.match.? \d*?$/)) This.value=this.t_value;else this.t_value=this.value;if (This.value.match (?: [/^ (?: \. \d+)?)? $/)) This.o_value=this.value "onkeyup=" if (!this.value.match (/^[\+\-]?\d*?\.? \d*?$/)) This.value=this.t_value;else this.t_value=this.value;if (This.value.match (?: [/^ (?: \. \d+)?)? $/)) This.o_value=this.value "onblur=" if (!this.value.match (?: [/^ (?: \). \d+)? | \.\d*?)? $/) this.value=this.o_value;else{if (This.value.match (/^\.\d+$/)) this.value=0+this.value;if (This.value.match (/^ \.$/)) This.value=0;this.o_value=this.value} ">
Encapsulated as a separate function:
Copy CodeThe code is as follows:
function KeyPress (ob) {
if (!ob.value.match (/^[\+\-]?\d*?\.? \d*?$/)) Ob.value = Ob.t_value; else Ob.t_value = Ob.value; if (Ob.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)?)? $/)) Ob.o_value = Ob.value;
}
function KeyUp (ob) {
if (!ob.value.match (/^[\+\-]?\d*?\.? \d*?$/)) Ob.value = Ob.t_value; else Ob.t_value = Ob.value; if (Ob.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)?)? $/)) Ob.o_value = Ob.value;
}
function OnBlur (ob) {
if (!ob.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)? | \.\d*?)? $/) ob.value=ob.o_value;else{if (Ob.value.match (/^\.\d+$/)) ob.value=0+ob.value;if (Ob.value.match (/^\.$/)) Ob.value=0;ob.o_value=ob.value};
}
Just call, pass in the This object!
4, only letters and kanji can be entered
Copy CodeThe code is as follows:
<input onkeyup= "Value=value.replace (/[\d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[\d]/g, ') "maxlength=10 name=" Numbers ">
5, can only input English letters and numbers, cannot input Chinese
Copy CodeThe code is as follows:
<input onkeyup= "Value=value.replace (/[^\w\.\/]/ig, ')" >
6, can only enter numbers and English
Copy CodeThe code is as follows:
<input onkeyup= "Value=value.replace (/[^\d|chun]/g, ')" >
7, only a maximum of two digits after the decimal point (numbers, Chinese can be entered), can not enter letters and operation symbols:
Copy CodeThe code is as follows:
<input onkeypress= "if ((event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/\.\d\d$/.test ( Value)) Event.returnvalue=false ">
8, only a maximum of two digits after the decimal point (numbers, letters, Chinese can be entered), you can enter the operation symbol:
Copy CodeThe code is as follows:
<input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') ">
Input box restricts only positive integers, logical AND and OR arithmetic