A veces necesita restringir el tipo de entrada de contenido en el cuadro de texto. En esta sección, la expresión regular restringe el cuadro de texto para ingresar solo números, puntos decimales, letras inglesas, caracteres chinos y otros códigos.
Por ejemplo, ingrese un entero positivo mayor 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. Solo se pueden ingresar códigos numéricos en el cuadro de texto (tampoco se puede ingresar el punto decimal)
<input onKeyUp = "this.value = this.value.replace (// d/g, '')" onAfterPaste = "this.value = this.value.replace (// d/g, '')">>
2. Solo ingrese números y puede ingresar puntos decimales. Es decir, solo
<input onKeyUp = "if (isnan (value)) execCommand ('UndO')" onAfterPaste = "if (isNan (value)) ExecCommand ('UndO')">
<input name = txt1 onchange = "if (// d/.test (this.value)) {alerta ('solo ingrese números'); this.value = '';}">
3. Números y puntos decimales Método dos
<Input type = text 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 en funciones separadas:
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;} KeyUp (ob) {if (! Ob.value.match (/^///-]?/D*?/D*?/D*? $/)) Ob.value = ob.value; else ob.t_value = ob.Value; if (ob.Value.match (/^(?: [/+/-]?/D+(?: /./ d+)?)? $/)) ob.o_value = ob.value; } función 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};}¡Simplemente pase este objeto en la llamada!
4. Solo ingrese letras y caracteres chinos
<input onKeyUp = "value = value.replace (/[/d]/g, '')" onbeforePaste = "PortboardData.SetData ('Text', PortboardData.getData ('Text'). Reemplazar (/[/d]/g, '' '))" MaxLength = 10 Name = "Numbers">
5. Solo ingrese letras y números en inglés, no chino
<input onkeyup = "value = value.replace (/[^/w /.//font>/ ig, '')">
6. Solo ingrese números e inglés
<input onKeyUp = "value = value.replace (/[^/d | chun]/g, '')">
7. Solo puede haber hasta dos dígitos después del punto decimal (se pueden ingresar números y chinos), y las letras y los símbolos del operador no se pueden ingresar:
<input onkeyPress = "if ((event.KeyCode <48 || Event.KeyCode> 57) && Event.KeyCode! = 46 || //./ D/D $/. Test (valor)) Event.returnValue = false">
8. Solo puede haber hasta dos dígitos después del punto decimal (números, letras y chinos), y se pueden ingresar símbolos del operador:
<input onkeyup = "this.value = this.value.replace (/^(/-)*(/d+)/. (/d/d).*$/, '$ 1 $ 2. $ 3')">
Esto se trata de este artículo. Los amigos que lo necesitan pueden referirse a él.