Foros del Web » Programando para Internet » Javascript »

Validacion onkeypress

Estas en el tema de Validacion onkeypress en el foro de Javascript en Foros del Web. Hola a todos, tengo un problemilla con una validacion, lo que pasa es que quiero validar un cuadro de texto al mometo de estar escribiendo, ...
  #1 (permalink)  
Antiguo 28/08/2009, 12:32
 
Fecha de Ingreso: mayo-2009
Mensajes: 4
Antigüedad: 14 años, 11 meses
Puntos: 0
Validacion onkeypress

Hola a todos, tengo un problemilla con una validacion, lo que pasa es que quiero validar un cuadro de texto al mometo de estar escribiendo, y lo que quiero hacer es validar numeros con punto decimal, tengo este codigo:

//js
function validar(e) {
tecla = (document.all)?e.keyCode:e.which;
if (tecla==8 || tecla==0){
return true;
}
patron = /([0-9\.])/;
te = String.fromCharCode(tecla);
return patron.test(te);
}
//html
<input type="text" name="pagoTxt" id="pagoTxt" onKeyPress="return validar(event)" value="" />

funciona bien solo acepta los numeros y el punto, pero me acepta indefinidamente el punto y lo que quiero hacer es que solo me acepte una vez el punto y enseguida digitos,

De antemano gracias por su ayuda, y cualquier comentario es muy bien recibido,
  #2 (permalink)  
Antiguo 28/08/2009, 14:43
Avatar de uselox  
Fecha de Ingreso: agosto-2008
Ubicación: Lima, Perú
Mensajes: 168
Antigüedad: 15 años, 7 meses
Puntos: 12
Respuesta: Validacion onkeypress

prueva con algo asi:
Código HTML:
<script language="javascript">
function validarNum(event, element, _float){
	event = event || window.event;
	var charCode = event.which || event.keyCode;
	if (charCode == 8 || charCode == 13 || (_float ? (element.value.indexOf('.') == -1 ? charCode == 46 : false) : false))
		return true;
	else if ((charCode < 48) || (charCode > 57))
		return false;
	return true;
}
</script> 
...

Código HTML:
<!-- no decimales -->
<input type="text" onkeypress="validarNum(event, this)"/>

<!-- con decimales -->
<input type="text" onkeypress="validarNum(event, this, true)"/> 
  #3 (permalink)  
Antiguo 31/08/2009, 09:45
 
Fecha de Ingreso: mayo-2009
Mensajes: 4
Antigüedad: 14 años, 11 meses
Puntos: 0
Respuesta: Validacion onkeypress

Cita:
Iniciado por uselox Ver Mensaje
prueva con algo asi:
Código HTML:
<script language="javascript">
function validarNum(event, element, _float){
	event = event || window.event;
	var charCode = event.which || event.keyCode;
	if (charCode == 8 || charCode == 13 || (_float ? (element.value.indexOf('.') == -1 ? charCode == 46 : false) : false))
		return true;
	else if ((charCode < 48) || (charCode > 57))
		return false;
	return true;
}
</script> 
...

Código HTML:
<!-- no decimales -->
<input type="text" onkeypress="validarNum(event, this)"/>

<!-- con decimales -->
<input type="text" onkeypress="validarNum(event, this, true)"/> 
Gracias por tu ayuda me funciono de maravilla, solo ke tube que cambiar una linea, pero todo funciono bien

Código HTML:
<script language="javascript">
function validarNum(event, element, _float){
	//event = event || window.event;
	//var charCode = event.which || event.keyCode;
        var charCode = (document.all)?e.keyCode:e.which;
	if (charCode == 8 || charCode == 13 || (_float ? (element.value.indexOf('.') == -1 ? charCode == 46 : false) : false))
		return true;
	else if ((charCode < 48) || (charCode > 57))
		return false;
	return true;
}
</script> 
  #4 (permalink)  
Antiguo 03/09/2009, 14:30
Avatar de uselox  
Fecha de Ingreso: agosto-2008
Ubicación: Lima, Perú
Mensajes: 168
Antigüedad: 15 años, 7 meses
Puntos: 12
Respuesta: Validacion onkeypress

Nueva Funcion:
Código Javascript:
Ver original
  1. var validarNum = function (event, _float){
  2.     event = event || window.event;
  3.     var charCode = event.keyCode || event.which;
  4.     var first = (charCode <= 57 && charCode >= 49);
  5.     if(_float){
  6.         var element = event.srcElement || event.target;
  7.         return first || (element.value.indexOf('.') == -1 ? charCode == 46 : false);
  8.     }
  9.     return first;
  10. }

...

Código HTML:
<!-- no decimales -->
<input type="text" onkeypress="validarNum(event)"/>

<!-- con decimales -->
<input type="text" onkeypress="validarNum(event, true)"/> 

Última edición por uselox; 29/12/2009 a las 13:21
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 22:46.