Hola:
Tengo dos funciones de javascript en dos formularios; en este primero cuanto el numero de carracteres en el campo texto "cosa"
Código PHP:
Ver original<html>
<head>
<title>Untitled</title>
<script>
function contar(esto){
cuantas=esto.length
if(cuantas>99){
document.forms[0].cosa.value=esto.substring(0,99)
}
document.forms[0].total.value=cuantas;
}
function validaFrm(){
var cosa = document.forms[0].cosa.value;
if(document.forms[0].cosa.value == "" || cosa.length < 15){
alert('Ingreso al menos 15 caracteres');
document.forms[0].cosa.focus();
return false;
} else if(cosa.length > 100){
alert('El número de caracteres es mayor a 100');
document.forms[0].cosa.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form method="post" action="directorio_enlace/contacto_envio.php" onsubmit="return validaFrm()">
<table width="515" style="color:#00FFFF " align="center" >
<tr>
<td colspan="2"><div align="center" >Nombre:<br>
<input type="text" name="nombre"size="40" value="<? echo $_GET['nombre']; ?>"/>
</div></td>
<td colspan="2">
<div align="center" >Email: <br>
<input name="email" type="text" size="40" value="<? echo $_GET['email']; ?>"/>
</div></td>
</tr>
<tr>
<td colspan="4"><div align="center" >Comentario:<br>
<textarea cols="40" rows="6" name="cosa" onkeydown="contar(this.value)"><? echo $_GET['cosa']; ?></textarea>
<input type="text" name="total" id="total" size="3" maxlength="3" value=0 readonly>
</div></td>
</tr>
<tr>
<td colspan="4"><div align="center" >Código de seguridad:<img src="directorio_enlace/captcha.php" align="middle"/>
<input type="text" name="codigo_verificacion" style="width: 50px;" />
</div>
</td>
</tr>
<tr>
<td colspan="2"><div align="center" ><input type="reset" name="enviar" value="Borrar datos" /> </div></td>
<td colspan="2"> <div align="center" ><input type="submit" name="enviar" value="Enviar Mensaje" /> </div></td>
</tr>
</table>
</form>
</body>
</html>
En este segundo, comprueba que el email sea "valido" y que una serie de campos esten rellenos
Código PHP:
Ver original<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' tiene que ser un email valido.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>
</head>
<body>
<form method="post" action="contacto_envio.php" onsubmit="MM_validateForm('nombre','','R','email','','RisEmail','codigo_verificacion','','R','mensaje','','R');return document.MM_returnValue">
<table width="515" style="color:#00FFFF " align="center" >
<tr>
<td colspan="2"><div align="center" >Nombre:<br>
<input type="text" name="nombre"size="40" value="<? echo $_GET['nombre']; ?>"/>
</div></td>
<td colspan="2">
<div align="center" >Email: <br>
<input name="email" type="text" size="40" value="<? echo $_GET['email']; ?>"/>
</div></td>
</tr>
<tr>
<td colspan="4"><div align="center" >Comentario:<br>
<textarea name="mensaje" cols="40" rows="6"><? echo $_GET['mensaje']; ?></textarea>
</div></td>
</tr>
<tr>
<td colspan="4"><div align="center" >Código de seguridad:<img src="captcha.php" align="middle"/>
<input type="text" name="codigo_verificacion" style="width: 50px;" />
</div>
</td>
</tr>
<tr>
<td colspan="2"><div align="center" ><input type="reset" name="enviar" value="Borrar datos" /> </div></td>
<td colspan="2"> <div align="center" ><input type="submit" name="enviar" value="Enviar Mensaje" /> </div></td>
</tr>
</table>
</form>
</body>
</html>
No he trabajado nunca con javascript, mas alla de copiar y pegar con lo cual es un mundo desconocido para mi.
Un saludo