テキストボックスのコンテンツ入力の種類を制限する必要がある場合があります。このセクションでは、正規表現では、テキストボックスが数字、小数点、英語の文字、漢字、その他のコードのみを入力するように制限しています。
たとえば、0より大きい正の整数を入力します
<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.数値コードのみをテキストボックスに入力できます(小数点も入力できません)
<input onkeyup = "this.value = this.value.replace(// d/g、 '')" onafterpaste = "this.value = this.value.replace(// d/g、 '')">
2。数字のみを入力し、小数点を入力できます。つまりのみ
<input onkeyup = "if(isnan(value))execcommand( 'undo')" onafterpaste = "if(isnan(value))execcommand( 'undo')">
<input name = txt1 onchange = "if(// d/.test(this.value)){alert( 'のみnumbers'); this.value = '';}">
3。数字と小数点法2
<入力型= text t_value = "" o_value = "" onkeypress = "if(!this.value.match(/^[/+/ - ]?/d*?/。?/d*?$/))this.value = this.t_value; els this.t_value = this.value; if(this.value.match(/^(?:[/+/ - ]?/d+(?:/./ d+)??$/))this.o_value = this.value " onkeyup = "if(!this.value.match(/^[/+/ - ]?/d*?/d*?/d*?$/))this.value = this.t_value; else; this.t_value = this.value; if(this.value.match(/^(?:[/+/ - ]?/d+(?:/./ d+)??$/))this.o_value = this.value " onblur = "if(!this.value.match(/^(?:[/+/ - ]?/d+(?:/./ d+)? .match(/^/./ d+$/))this.value = 0+this.value; if(this.value.match(/^/.$/))this.value = 0; this.o_value = this.value} ">
個別の関数にカプセル化されています:
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+)??)?$/)) 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};};}このオブジェクトを通話内で渡すだけです!
4.文字と漢字のみを入力します
<input onkeyup = "value = value.replace(/d]/g、 '')" onbeforepaste = "lipboarddata.setdata( 'text'、clipboarddata.getdata( 'text')
5.中国語ではなく、英語の手紙と数字のみを入力します
<input onkeyup = "value = value.replace(/[^/w /.// ]/ ig、 '')">
6.数字と英語のみを入力します
<input onkeyup = "value = value.replace(/[^/d | chun]/g、 '')">
7.小数点(数字と中国語を入力できます)の後に最大2桁しかありません。また、文字と演算子のシンボルを入力できません。
<input onkeypress = "if((event.keycode <48 || event.keycode> 57)&& event.keycode!= 46 || //./ d/d $/。test(value))event.returnvalue = false">
8.小数点(数字、文字、中国語)の後に最大2桁しかありません。また、オペレーターのシンボルを入力できます。
<input onkeyup = "this.value = this.value.replace(/^(/ - )*(/d+)/。(/d/d)。*$/、 '$ 1 $ 2. $ 3')">
これはすべてこの記事です。それを必要とする友達はそれを参照できます。