때로는 텍스트 상자의 컨텐츠 입력 유형을 제한해야합니다. 이 섹션에서 정규 표현식은 텍스트 상자가 숫자, 소수점, 영어 문자, 한자 및 기타 코드 만 입력하도록 제한합니다.
예를 들어, 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')"onfterpaste = "if (isnan (value)) execcommand ( 'undo')">
<input name = txt1 onchange = "if (// d/.test (this.value)) {alert ( 'enther numbers'); this.value = '';}">
3. 숫자와 소수점 포인트 방법 2
<입력 유형 = 텍스트 t_value = ""o_value = ""onkeypress = "if (! this.value.match (/^[/+/-]?/d*?/.?/d*? $/)) this.value = this.t_value; else 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+)? | /./ 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} ">
별도의 기능으로 캡슐화 :
함수 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.match (/^[/+/-]?/d*?/d*?/d*?/d*? else ob.t_value = ob.value; if (ob.value.match (/^(? : [/+/-]?/-]?/d+(? : /./ d+)?)) ob.o_value = ob.value; } 함수 onblur (OB) {if (! ob.value.match (/^(? : [/+/-]? : /./ 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 = "clipboarddata.setdata ( 'text', clipboarddata.getData ( 'text'). 대체 (/[/d]/g, ''))"maxlength = 10 name = "숫자">
5. 중국어가 아닌 영어 편지와 숫자 만 입력하십시오
<input onkeyup = "value = value.replace (/[^/w /./ ]/ ig, '')">
6. 숫자와 영어 만 입력하십시오
<input onkeyup = "value = value.replace (/[^/d | chun]/g, '')">
7. 소수점 후에는 최대 두 자리 만있을 수 있으며 (숫자와 중국어를 입력 할 수 있음) 문자와 운영자 기호를 입력 할 수 없습니다.
<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')">
이것은이 기사에 관한 것입니다. 필요한 친구는 그것을 참조 할 수 있습니다.