Foros del Web » Programando para Internet » Javascript »

impedir ingreso de caracteres en text

Estas en el tema de impedir ingreso de caracteres en text en el foro de Javascript en Foros del Web. Hola amigos, otra vez molestando resulta que todavia estoy en lo de el ping y ya he avanzado mucho haciendo las validaciones, que no se ...
  #1 (permalink)  
Antiguo 13/05/2008, 08:42
Avatar de jhonmario  
Fecha de Ingreso: marzo-2008
Mensajes: 22
Antigüedad: 16 años, 1 mes
Puntos: 0
impedir ingreso de caracteres en text

Hola amigos, otra vez molestando resulta que todavia estoy en lo de el ping y ya he avanzado mucho haciendo las validaciones, que no se ingresen puntos, que no sea mayor a 255 que tenga 4 octetos etc. y tambien que no tenga caracteres, pero ahi es donde tengo el problema he logrado que no se ingresen caracteres ejemplo
sdfssdfs o 192.168.1.AA pero cuando ingreso una ip con con caracteres combinados con numeros en un octeto ejemplo 192.168.1.1AA no puedo impedir que no me lo valide. A ver si me pueden ayudar aqui dejo el código completo

Cita:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PING</title>
<script language="JavaScript" type="text/JavaScript" src="ping.js">
</script>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 250px;
margin-right: 0px;
margin-bottom: 0px;
}</style>
</head>

<body bgcolor="#000000">

<form name="formulario" id="formulario" action="ping.php" method="post" onSubmit="return validar(this);">

<table border="1" align="center" cellspacing="0" cellpadding="0">
<tr>
<td width="302" align="center">
<span style="color: white;"><strong>REALIZAR PING</strong></span>
</td>
</tr>
</table>


<table border="1" align="center" cellspacing="0" cellpadding="0">
<tr>
<td width="150" align="center">
<span style="color: blue;"><strong>INGRESE IP CPE:</strong></span><br>
</td>
<td width="150" align="center">
<input type="text" name="cpe" id="cpe">
</td>
</tr>

<tr>
<td width="150" align="center">
<span style="color: blue;"><strong>INGRESE IP VPN:</strong></span><br>
</td>
<td width="150" align="center">
<input type="text" name="vpn" id="vpn">
</td>
</tr>

<table border="1" align="center" cellspacing="0" cellpadding="0">
<tr>
<td width="302" align="center">
<input type="submit" name="PING" id="PING" value="Iniciar"/>
</td>
</tr>
</table>

</table>
</form>
</body>
</html>
Código PHP:
<?php
if (isset ($_POST))
{
  foreach (
$_POST as $inputName  => $value_)
  {
   $
$inputName $value_;
  }
}
else
{
  foreach (
$HTTP_POST_VARS as $inputName  => $value_)
  {
   $
$inputName $value_;
  }
}

if (isset (
$_GET))
{
  foreach (
$_GET as $inputName  => $value_)
  {
   $
$inputName $value_;
  }
}
else
{
  foreach (
$HTTP_GET_VARS as $inputName  => $value_)
  {
   $
$inputName $value_;
  }
}

$comando "c:\windows\system32\ping " $cpe;
$salida shell_exec($comando);
$JOIN=split("\n",$salida);

$comando2 "c:\windows\system32\ping " $vpn;
$salida2 shell_exec($comando2);
$JOIN2=split("\n",$salida2);

//echo "$comando2";
echo "<html>";
echo 
"<body bgcolor=\"#000000\" text=\"#FFFFFF\" link=\"#FFFFFF\" vlink=\"#EEEEEE\" LINK=\"#EEEEEE\" background=\"\">";

$ip $_SERVER["REMOTE_ADDR"]; 
echo 
"<b><h2>Su Dirección IP es :"$ip." </h2>";
$color "";

if (
substr_count ($salida,"Respuesta") > 0)
{
  
$color "green";
  
/*
     echo "<font color='green'> PING A " . $cpe. " EXITOSO </font><br>";

     for($i=0; $i<sizeof($JOIN); $i++)
     {
        echo "<font color='green'>" . $JOIN[$i]. "</font><br>";
        //ob_flush();
        flush();
        usleep(300000);
     }
     */
}
else
{  
$color "red";
  
/*
     echo "<font color='red'> PING A " . $cpe. " FALLIDO </font><br>";
     
     for($i=0; $i<sizeof($JOIN); $i++)
     {
        echo "<font color='red'>" . $JOIN[$i]. "</font><br>";
        //ob_flush();
        flush();
        usleep(300000);
     }     
     */
}

$color_1 "";

if (
substr_count ($salida2,"Respuesta") > 0)
{
$color_1 "green";
  
/*
     echo "<font color='green'> PING A " . $vpn. " EXITOSO </font><br>";

   for($i=0; $i<sizeof($JOIN2); $i++)
     {  
        echo "<font color='green'>" . $JOIN2[$i]. "</font><br>";
        //ob_flush();
        flush();
        usleep(300000);
     }
     */
}
else
{
  
$color_1 "red";
  
/*
   echo "<font color='red'> PING A " . $vpn. " FALLIDO </font><br>";
   
   for($i=0; $i<sizeof($JOIN2); $i++)
     {  
        echo "<font color='red'>" . $JOIN2[$i]. "</font><br>";
        //ob_flush();
        flush();
        usleep(300000);
     }
     */
}


echo 
"<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >";

   for(
$i=0$i 12$i++)
     {
       echo 
"<tr>";
       
       if (
$i == 1)
       {
        echo 
"<td width=\"50%\">";
        echo 
"<font color='yellow'>" $JOIN[$i]. "</font><br>"
        echo 
"</td>";
        echo 
"<td width=\"50%\">";
        echo 
"<font color='yellow'>" $JOIN2[$i]. "</font><br>"
        echo 
"</td>";
       }
       else
       {
        if (isset (
$JOIN[$i]))
        {
        echo 
"<td width=\"50%\">";         
        echo 
"<font color='".$color."'>" $JOIN[$i]. "</font><br>";
        echo 
"</td>";
        }
        else
        {
        echo 
"<td width=\"50%\">";         
         echo 
"<font color='".$color."'></font><br>";       
        echo 
"</td>";
        }
     

        if (isset (
$JOIN2[$i]))
        {
        echo 
"<td width=\"50%\">";
        echo 
"<font color='".$color_1."'>" $JOIN2[$i]. "</font><br>";         
        echo 
"</td>";
        }
        else
        {
        echo 
"<td width=\"50%\">";
         echo 
"<font color='".$color_1."'></font><br>";       
        echo 
"</td>";
        }
       }
       
       echo 
"</tr>";
       
flush();
       
usleep(300000); 
     }

echo 
"</table>";


  
//meter en tablas
  //Volver a tirar el mismo test
    
//echo $salida
echo "</body></html>";
?>

<form name="ping" id="ping" action="index.php" method="post">
<input type="hidden" name="$vpn" id="$vpn" ><br>
<input type="hidden" name="$cpe" id="$cpe"><br>
<input type="submit" name="volver" value="Nuevo Ping"><br>
</form>
EL CODIGO JS

Cita:
function validar()
{
with (document.forms['formulario'])
{
if(cpe.value == "")
{
alert("Ingrese la ip CPE");
cpe.focus();
return false;
}
else
{
partes=cpe.value.split('.');

if (partes.length != 4)
{
alert('Ip no valida, Faltan o hay demasiados Valores');
cpe.focus();
return false;
}
else
{
for (i = 0; i < 4; i++)
{
miInteger = parseInt(partes[i]);

if (isNaN (miInteger))
{
alert('No puede ingresar caracteres');
cpe.focus();
return false;
}
else
{
if (partes[i] > 255 || partes[i].length == 0)
{
alert('No puede ingresar valores mayores a 255');
cpe.focus();
return false;
}
}
}
//return true;
}
}

if(vpn.value == "")
{
alert("Ingrese la ip CPE");
vpn.focus();
return false;
}
else
{
partes=vpn.value.split('.');

if (partes.length != 4)
{
alert('Ip no valida, Faltan o hay demasiados Valores');
vpn.focus();
return false;
}
else
{
for (i = 0; i < 4; i++)
{
miInteger = parseInt(partes[i]);

if (isNaN (miInteger))
{
alert('No puede ingresar caracteres');
vpn.focus();
return false;
}
else
{
if (partes[i] > 255 || partes[i].length == 0)
{
alert('No puede ingresar valores mayores a 255');
vpn.focus();
return false;
}
}
}
return true;
}
}
}
}
  #2 (permalink)  
Antiguo 13/05/2008, 08:56
 
Fecha de Ingreso: septiembre-2005
Mensajes: 840
Antigüedad: 18 años, 7 meses
Puntos: 84
Re: impedir ingreso de caracteres en text

Código PHP:
var regeX = new RegExp('.*[a-zA-Z].*') ;

var 
ip "123.212.111.1A1" ;

if (
ip.search(regeX) != -1)
         
alert('Ip Invalida') ; 

No me acuerdo bien de lo de las EXp regulares en javascript, pero creo que era as i

Eso significa que si encuentra alguna letra, sea cual sea, y este rodeada por lo que sea, salte el alert
__________________
» Presupuesto Pagina Web
  #3 (permalink)  
Antiguo 13/05/2008, 08:59
Avatar de jhonmario  
Fecha de Ingreso: marzo-2008
Mensajes: 22
Antigüedad: 16 años, 1 mes
Puntos: 0
Re: impedir ingreso de caracteres en text

Cita:
Iniciado por miSko Ver Mensaje
Código PHP:
var regeX = new RegExp('.*[a-zA-Z].*') ;

var 
ip "123.212.111.1A1" ;

if (
ip.search(regeX) != -1)
         
alert('Ip Invalida') ; 

No me acuerdo bien de lo de las EXp regulares en javascript, pero creo que era as i

Eso significa que si encuentra alguna letra, sea cual sea, y este rodeada por lo que sea, salte el alert

OK, voy a intentar a ver como me funciona y te comento
  #4 (permalink)  
Antiguo 13/05/2008, 09:30
 
Fecha de Ingreso: septiembre-2005
Mensajes: 840
Antigüedad: 18 años, 7 meses
Puntos: 84
Re: impedir ingreso de caracteres en text

Ostia, ahora q la pienso mejor, igual era solo esto :

var regeX = new RegExp('[a-zA-Z]+') ;

Voy confundiendo lenguajes xDD
__________________
» Presupuesto Pagina Web
  #5 (permalink)  
Antiguo 13/05/2008, 09:47
Avatar de jhonmario  
Fecha de Ingreso: marzo-2008
Mensajes: 22
Antigüedad: 16 años, 1 mes
Puntos: 0
Re: impedir ingreso de caracteres en text

Cita:
Iniciado por miSko Ver Mensaje
Ostia, ahora q la pienso mejor, igual era solo esto :

var regeX = new RegExp('[a-zA-Z]+') ;

Voy confundiendo lenguajes xDD
Me confundi, en que parte de mi función validar va esa linea?:
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 23:30.