Às vezes, você precisa restringir o tipo de entrada de conteúdo na caixa de texto. Nesta seção, a expressão regular restringe a caixa de texto para inserir apenas números, pontos decimais, letras em inglês, caracteres chineses e outros códigos.
Por exemplo, insira um número inteiro positivo maior que 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. Somente códigos numéricos podem ser inseridos na caixa de texto (o ponto decimal também não pode ser inserido)
<input onKeyup = "this.value = this.value.replace (// d/g, '')" ONAFTERPASTE = "this.value = this.value.replace (// d/g, '')">
2. Somente insira números e pode inserir pontos decimais. Ou seja, apenas
<input onkeyup = "if (isnan (value)) ExecCommand ('undo')" OnAfterPaste = "if (isnan (value)) ExecCommand ('undo')">
<nome de entrada = txt1 onchange = "if (// d/.test (this.value)) {alert ('apenas digite números'); this.value = '';}">
3. Números e pontos decimais Método dois
<tipo de entrada = texto 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} ">
Encapsulado em funções separadas:
função 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;} função keyup (ob) {if (! ob.value.match (/^[/+/-]?/d*?/d*?/d*? else ob.t_value = ob.value; if (ob.value.match (/^(?: [/+/-]?/d+(?: /./ d+)?)? $/)) ob.o_value = ob.value; } função onblur (ob) {if (! ob.value.match (/^(?: [/+/-]?/d+(?: /./ d+)? | /./ d*?)? $/)) ob.value = ob.o_value; else {if (ob.value. corresponder (/^/./ d+$/)) ob.value = 0+ob.value; if (ob.value.match (/^/.$/)) ob.value = 0; ob.o_value = ob.value};}Basta passar este objeto na chamada!
4. Somente insira cartas e caracteres chineses
<input onkeyup = "value = value.replace (/[/d]/g, '')" onBeforePaste = "clipboarddata.setData ('texto', clipboarddata.getData ('text'). substituir (/[/d]/g, '')" maxlengthing = 10 name = "Numbers)
5. Somente insira cartas e números em inglês, não chineses
<input onkeyup = "value = value.Replace (/[^/w /.//bys
6. Somente insira números e inglês
<input onkeyup = "value = value.replace (/[^/d | chun]/g, '')">
7. Só pode haver até dois dígitos após o ponto decimal (números e chinês podem ser inseridos), e as cartas e os símbolos do operador não podem ser inseridos:
<input onkeypress = "if ((event.keycode <48 || event.keycode> 57) && event.keycode! = 46 || //./ d/d $/. test (value)) event.returnValue = false">
8. Só pode haver até dois dígitos após o ponto decimal (números, letras e chinês), e os símbolos do operador podem ser inseridos:
<input onKeyup = "this.value = this.value.replace (/^(/-)*(/d+)/. (/d/d).*$/, '$ 1 $ 2. $ 3')">
Isso é tudo sobre este artigo. Amigos que precisam podem se referir a ele.