Foros del Web » Programando para Internet » Javascript »

seguridad en formulario

Estas en el tema de seguridad en formulario en el foro de Javascript en Foros del Web. Hola: Estoy realizando la seguridad de mi formulario de registro para que el usuario no me ingrese caracteres extraños. He mirado algunas funciones y las ...
  #1 (permalink)  
Antiguo 31/12/2011, 02:29
 
Fecha de Ingreso: octubre-2011
Mensajes: 160
Antigüedad: 12 años, 6 meses
Puntos: 2
seguridad en formulario

Hola:

Estoy realizando la seguridad de mi formulario de registro para que el usuario no me ingrese caracteres extraños. He mirado algunas funciones y las he adaptado a mi página, pero no me funciona ninguna, no sé si es que están mal formuladas o es el botón submit (está al final del código) el que está mal, porque ingreso caracteres no permitidos, y se envía el formulario a la base de datos, ¿me podéis ayudar?. Muchas gracias.

Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Documento sin título</title>
<
link href="estilos.css" rel="stylesheet" type="text/css" />
<
link href="registro.css" rel="stylesheet" type="text/css" />

<
script type="text/javascript">  
<!--   
function 
validatePass($nombreamo) {   
   var 
RegExPattern = /(?!^[0-9]*$)(?!^[a-zA-Z áéíóúñÑÁÉÍÓÚü]*$)^([a-zA-Z áéíóúñÑÁÉÍÓÚü 0-9]{3,25})$/;   
    var 
errorMessage 'Nombre Incorrecto.'
    return 
false    //no submit  
   
if ((campo.value.match(RegExPattern)) && (campo.value!='')) {   
   } else {   
        
alert(errorMessage);   
    }    
}   

function 
validarEmail($email) {  
if (/^
w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3,4})+$/.test($email)){  
   } else {  
    
alert("La dirección de email es incorrecta."); 
    return 
false    //no submit  

 
}
}



function 
ValidaCampos($telefono)

//comprueba campo teléfono de formulario
//usa el metodo test de expresion regular

if(/^([0-9+s+-])+$/.test($telefono)) {
}
else
{
alert('El número de teléfono ingresado no es válido.');
return 
false//no submit
}
}

//-->  
</script> 


</head>

<body class="fondoregistro">
<form action="registrandose.php" method="post" enctype="multipart/form-data" id="formulario">
  <table width="1024" height="774" border="0">
    <tr>
      <td width="312" rowspan="10" >&nbsp;</td>
      <td width="349" height="90">&nbsp;</td>
      <td width="349" height="90">&nbsp;</td>
    </tr>
    <tr>
      <td height="30"><label for="pais"></label>
        <label for="ciudad"></label>
      <input name="pais" type="text" class="campotexto2" id="textfield3" maxlength="25" /></td>
      <td height="30">
        <table width="76%" border="0">
          <tr>
            <td width="50%" height="31">
            <input type="radio" class="opcionpedigri" id="radio" value="si" checked name="pedigri"/>
            <label for="si"></label></td>
            <td width="50%"><input name="pedigri" type="radio" class="opcionpedigri" id="radio2" value="no" />
            <label for="no"></label></td>
          </tr>
      </table></td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
    </tr>
    <tr>
      <td height="30"><label for="zona"></label>
      <input name="provincia" type="text" class="campotexto" id="textfield4" maxlength="25" /></td>
      <td height="30"><label for="telefono"></label>
      <input name="capa" type="text" class="campotexto" id="textfield11" maxlength="25" /></td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
    </tr>
    <tr>
      <td height="30"><label for="especie"></label>
      <input name="ciudad" type="text" class="campotexto" id="textfield5" maxlength="25" /></td>
      <td height="30"><label for="email"></label>
      <input name="nombreamo" type="text" class="campotexto" id="textfield12" maxlength="25" /></td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
    </tr>
    <tr>
      <td height="30"><label for="raza"></label>
      <input name="zona" type="text" class="campotexto" id="textfield6" maxlength="25" /></td>
      <td height="30"><label for="usuario"></label>
      <input name="telefono" type="text" class="campotexto" id="textfield13" maxlength="15" /></td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
    </tr>
    <tr>
      <td height="30"><label for="edad"></label>
      <input name="especie" type="text" class="campotexto" id="textfield7" maxlength="25" /></td>
      <td height="30"><label for="contrasenna"></label>
      <input name="email" type="text" class="campotexto" id="textfield14" maxlength="40" /></td>
    </tr>
    <tr>
      <td height="45"><label for="foto"></label>
      <input name="foto" type="file" class="foto" id="foto" size="30" /></td>
      <td height="45">&nbsp;</td>
      <td height="45">&nbsp;</td>
    </tr>
    <tr>
      <td rowspan="6">&nbsp;</td>
      <td height="30"><label for="sexo"></label>
      <input name="raza" type="text" class="campotexto" id="textfield8" maxlength="25" /></td>
      <td height="30"><label for="repcontrasenna"></label>
      <input name="usuario" type="text" class="campotexto" id="textfield15" maxlength="25" /></td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
    </tr>
    <tr>
      <td height="30"><label for="capa"></label>
      <input name="edad" type="text" class="campotexto" id="textfield9" maxlength="2" /></td>
      <td height="30"><label for="textfield16"></label>
      <input name="contrasenna" type="password" class="campotexto" id="textfield16" maxlength="25" /></td>
    </tr>
    <tr>
      <td height="30">&nbsp;</td>
      <td height="30">&nbsp;</td>
    </tr>
    <tr>
      <td height="31"><label for="nombreamo"></label>
        <label for="sexo"></label>
        <select name="sexo" size="1" class="campotexto" id="sexo">
          <option selected="selected">macho</option>
          <option>hembra</option>
      </select></td>
      <td height="31"><label for="textfield17"></label>
      <input name="repcontrasenna" type="password" class="campotexto" id="textfield17" maxlength="25" /></td>
    </tr>
    <tr>
      <td height="45">&nbsp;</td>
      <td ><input type="submit" name="button" id="button" value="Enviar" onsubmit="return validatePass($nombreamo), validarEmail($email), ValidaCampos($telefono);"/></td>
    </tr>
  </table>
</form>
</body>
</html> 
  #2 (permalink)  
Antiguo 31/12/2011, 03:43
Avatar de caricatos
Moderador
 
Fecha de Ingreso: abril-2002
Ubicación: Torremolinos (Málaga)
Mensajes: 19.607
Antigüedad: 22 años
Puntos: 1284
Respuesta: seguridad en formulario

Hola:

A simple vista tienes un par de cosas mal, aunque una de ellas podría colarse... ésta:
onsubmit es un manejador de evento del tag form, no del input, así que está mal ubicado.

Luego todas las validaciones devuelven true si son pasadas, pero no haces la concatenación lógica correcta... debería ser:

<form onsubmit="return (validatePass(this.nombreamo) && validarEmail(this.email) && ValidaCampos(this.telefono))"

Tampoco pones bien las referencias, no sé de donde has sacado lo de $email... en javascript no es normal/obligatorio el signo dolar para referenciar variables.
Cuando pones algo en la definición de las funciones, se supone que luego debes usarlo...

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #3 (permalink)  
Antiguo 31/12/2011, 09:01
 
Fecha de Ingreso: octubre-2011
Mensajes: 160
Antigüedad: 12 años, 6 meses
Puntos: 2
Respuesta: seguridad en formulario

Entonces, ¿la manera correcta de hacerlo sería ésta? (es que no sé si lo he entendido correctamente):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<link href="estilos.css" rel="stylesheet" type="text/css" />
<link href="registro.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
<!--
function validatePass(nombreamo) {
var RegExPattern = /(?!^[0-9]*$)(?!^[a-zA-Z áéíóúñÑÁÉÍÓÚü]*$)^([a-zA-Z áéíóúñÑÁÉÍÓÚü 0-9]{3,25})$/;
var errorMessage = 'Nombre Incorrecto.';
return false //no submit
if ((campo.value.match(RegExPattern)) && (campo.value!='')) {
} else {
alert(errorMessage);
}
}

function validarEmail(email) {
if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3,4})+$/.test($email)){
} else {
alert("La dirección de email es incorrecta.");
return false //no submit

}
}



function ValidaCampos(telefono)
{
//comprueba campo teléfono de formulario
//usa el metodo test de expresion regular

if(/^([0-9+s+-])+$/.test($telefono)) {
}
else
{
alert('El número de teléfono ingresado no es válido.');
return false; //no submit
}
}

//-->
</script>


</head>

<body class="fondoregistro">
<form onsubmit="return (validatePass(this.nombreamo) && validarEmail(this.email) && ValidaCampos(this.telefono))" action="registrandose.php" method="post" enctype="multipart/form-data" id="formulario">
<table width="1024" height="774" border="0">
<tr>
<td width="312" rowspan="10" >&nbsp;</td>
<td width="349" height="90">&nbsp;</td>
<td width="349" height="90">&nbsp;</td>
</tr>
<tr>
<td height="30"><label for="pais"></label>
<label for="ciudad"></label>
<input name="pais" type="text" class="campotexto2" id="textfield3" maxlength="25" /></td>
<td height="30">
<table width="76%" border="0">
<tr>
<td width="50%" height="31">
<input type="radio" class="opcionpedigri" id="radio" value="si" checked name="pedigri"/>
<label for="si"></label></td>
<td width="50%"><input name="pedigri" type="radio" class="opcionpedigri" id="radio2" value="no" />
<label for="no"></label></td>
</tr>
</table></td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td height="30">&nbsp;</td>
</tr>
<tr>
<td height="30"><label for="zona"></label>
<input name="provincia" type="text" class="campotexto" id="textfield4" maxlength="25" /></td>
<td height="30"><label for="telefono"></label>
<input name="capa" type="text" class="campotexto" id="textfield11" maxlength="25" /></td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td height="30">&nbsp;</td>
</tr>
<tr>
<td height="30"><label for="especie"></label>
<input name="ciudad" type="text" class="campotexto" id="textfield5" maxlength="25" /></td>
<td height="30"><label for="email"></label>
<input name="nombreamo" type="text" class="campotexto" id="textfield12" maxlength="25" /></td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td height="30">&nbsp;</td>
</tr>
<tr>
<td height="30"><label for="raza"></label>
<input name="zona" type="text" class="campotexto" id="textfield6" maxlength="25" /></td>
<td height="30"><label for="usuario"></label>
<input name="telefono" type="text" class="campotexto" id="textfield13" maxlength="15" /></td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td height="30">&nbsp;</td>
</tr>
<tr>
<td height="30"><label for="edad"></label>
<input name="especie" type="text" class="campotexto" id="textfield7" maxlength="25" /></td>
<td height="30"><label for="contrasenna"></label>
<input name="email" type="text" class="campotexto" id="textfield14" maxlength="40" /></td>
</tr>
<tr>
<td height="45"><label for="foto"></label>
<input name="foto" type="file" class="foto" id="foto" size="30" /></td>
<td height="45">&nbsp;</td>
<td height="45">&nbsp;</td>
</tr>
<tr>
<td rowspan="6">&nbsp;</td>
<td height="30"><label for="sexo"></label>
<input name="raza" type="text" class="campotexto" id="textfield8" maxlength="25" /></td>
<td height="30"><label for="repcontrasenna"></label>
<input name="usuario" type="text" class="campotexto" id="textfield15" maxlength="25" /></td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td height="30">&nbsp;</td>
</tr>
<tr>
<td height="30"><label for="capa"></label>
<input name="edad" type="text" class="campotexto" id="textfield9" maxlength="2" /></td>
<td height="30"><label for="textfield16"></label>
<input name="contrasenna" type="password" class="campotexto" id="textfield16" maxlength="25" /></td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td height="30">&nbsp;</td>
</tr>
<tr>
<td height="31"><label for="nombreamo"></label>
<label for="sexo"></label>
<select name="sexo" size="1" class="campotexto" id="sexo">
<option selected="selected">macho</option>
<option>hembra</option>
</select></td>
<td height="31"><label for="textfield17"></label>
<input name="repcontrasenna" type="password" class="campotexto" id="textfield17" maxlength="25" /></td>
</tr>
<tr>
<td height="45">&nbsp;</td>
<td ><input type="submit" name="button" id="button" value="Enviar"/></td>
</tr>
</table>
</form>
</body>
</html>

Última edición por cheterporras2; 31/12/2011 a las 09:08
  #4 (permalink)  
Antiguo 02/01/2012, 12:21
 
Fecha de Ingreso: octubre-2011
Mensajes: 160
Antigüedad: 12 años, 6 meses
Puntos: 2
Respuesta: seguridad en formulario

¿Alguien me podría decir si finalmente el código es correcto?

Etiquetas: formulario, javascript+php, seguridad
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 08:51.