Foros del Web » Programando para Internet » Javascript »

validar numeros

Estas en el tema de validar numeros en el foro de Javascript en Foros del Web. que tal como le hago para que un campo de mi formulario solo me acepte numeros y no me permita utilizar caracteres !!!...
  #1 (permalink)  
Antiguo 21/05/2002, 10:51
 
Fecha de Ingreso: mayo-2002
Mensajes: 299
Antigüedad: 22 años
Puntos: 0
validar numeros

que tal como le hago para que un campo de mi formulario solo me acepte numeros y no me permita utilizar caracteres !!!
  #2 (permalink)  
Antiguo 21/05/2002, 11:30
 
Fecha de Ingreso: mayo-2002
Mensajes: 197
Antigüedad: 22 años
Puntos: 0
Re: validar numeros

<form method="post" action="accseso.asp" onSubmit="return validar(this)">

<script Language="JavaScript">
<!--
function validar(formulario)
{

var checkOK = "0123456789,.' ";
var checkStr = formulario.NUMERO.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Escriba sólo números en el campo NUMERO.");
formulario.NUMERO.focus();
return (false);
}
// -->
</script>

Esto comprobará que solo se hayan escrito o números o "," o "." o "'" o " ", y se hay algún otra caracter que hará una alerta y te devolverá al campo NUMERO.

Saludos.
  #3 (permalink)  
Antiguo 21/05/2002, 11:57
bet
 
Fecha de Ingreso: febrero-2001
Mensajes: 292
Antigüedad: 23 años, 2 meses
Puntos: 0
Re: validar numeros

Código:
<html>
<head>
<title> Solo numeros </title>

<script language="JavaScript">
<!--
var nav4 = window.Event ? true : false;

function acceptNum(evt)
{
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57));
}
//-->
</script>
</head>

<body>
<input type=text name="valor" size=12 maxlength=12 onKeyPress="return acceptNum(event)"><br>

</body>
</html>
<hr noshade size=1><img src="http://www.gograph.com/Images-8712/ClipArt/cat03.gif" height="50" border=0 align="absmiddle"> <font size="2" face="verdana" color="#000000">bet[/CODE]
  #4 (permalink)  
Antiguo 22/05/2002, 00:28
Usuario no validado
 
Fecha de Ingreso: julio-2001
Mensajes: 766
Antigüedad: 22 años, 9 meses
Puntos: 0
Re: validar numeros

cheka esto mas sencillito


&lt;script language=&quot;Javascript&quot;&gt;
/*-- for(unKnown){ [email protected] } &lt;--do n't remove*/
function Validar(form)
{

if (isNaN(form.pregunta1.value))
{ alert(&quot;Porfa Solo numeros&quot;); ; return; }

}
&lt;/script&gt;




&lt;form action=&quot;null&quot;&gt;
&lt;input name=&quot;pregunta1&quot; type=&quot;text&quot; value=&quot;only numeros&quot;&gt;&lt;br&gt;
&lt;input type=&quot;button&quot; value=&quot;Enviar&quot; onClick=&quot;Validar(this.form)&quot;&gt;
&lt;/form&gt;
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




La zona horaria es GMT -6. Ahora son las 13:53.