Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/05/2009, 14:18
crisnueve
 
Fecha de Ingreso: noviembre-2008
Mensajes: 8
Antigüedad: 15 años, 6 meses
Puntos: 0
Validar formulario php llamando un .js

Buenas!
Estoy haciendo un formulario con php y postgres. He perdido varios dias intentando hacer la validacion, el problema es que la validacion debe ser desde un formulario php que llama a un archivo .js por ejemplo javascript.js, la validacion debe hacer lo siguiente:
Tengo diferentes campos, ya sean de solo numeros, de solo letras, e-mail, etc. La cosa es que al hacerle click al campo, este debe ponerse en rojo y cuando el usuario ingrese los caracteres correctos este sera de color blanco.


Tengo el siguiente formulario que quiero validar:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
<style type="text/css">
<!--
.Estilo1 {
color: #000099;
font-style: italic;
}
-->
</style>
</head>

<body>
<script language='javascript' src="javascripts.js"></script>
<table width="341" height="291" border="4" align="center" cellpadding="0" cellspacing="2" bordercolor="#000000" bgcolor="#CCCCCC">
<tr>
<td width="587" bgcolor="#CCCCCC"><form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<table width="200" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td height="50"><div align="center">Nombre </div></td>
<td height="50"><p align="center">
<input name="nombre" type="text" id="nombre" onFocus="obligatorio(this)" onKeyPress="obligatorio2(this)"/>
</p> </td>
</tr>
<tr>
<td height="50">Cedula</td>
<td height="50"><p>
<input name="cedula" type="text" id="cedula" onClick="return msn()" />
</p> </td>
</tr>
<tr>
<td height="50">Tipo Usuario</td>
<td height="50"><div align="center">
<select name="tipoUsuario">
<option selected>principal</option>
<option>consultor</option>
</select>
&nbsp;</div></td>
</tr>
</table>
<table width="200" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td><div align="right" class="Estilo1"><a href="cambiarContrasena.php">cambiar contrase&ntilde;a </a></div></td>
</tr>
</table>
<p>&nbsp;</p>
<table width="200" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td><div align="center">
<input name="ingresar" type="button" value="Ingresar">
&nbsp;</div></td>
</tr>
</table>
<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
</form> </td>
</tr>
</table>
</body>
</html>

Este esta conectado al archivo javascripts.js, las funciones que llamo desde el campo nombre me sirven, lo que hace es poner este campo en rojo y valida solo que no este vacio, cuando este no esta vacio cambia a blanco, las funciones que estan en el .js son estas:

function obligatorio(obj) {
if (obj.value=='') {
if ((document.all)||(document.getElementById)) {
obj.style.backgroundColor = '#993300';
}
}
else {
if ((document.all)||(document.getElementById)) {
obj.style.backgroundColor = '#FFFFFF';
}
}
}

function obligatorio2(obj) {
if (obj.value!='') {
if ((document.all)||(document.getElementById)) {
obj.style.backgroundColor = '#FFFFFF';
}
} else {
if ((document.all)||(document.getElementById)) {
obj.style.backgroundColor = '#993300';
}
}
}

La verdad no entiendo muy bien su funcionamiento, si me podrian comentar sobre el objeto document me gustaria mucho.

Y ahora en el campo cedula quise hacer algunas pruebas:

function msn(){
alert("Hola.");
}

Esta funcion esta en el archivo .js. La llamo desde el campo cedula por medio del evento onClick, trato de sacar ese mensaje pero no sale.

Como saco un mensaje hacia el formulacio desde un .js?

Y como hacer esta validacion de forma correcta?

Gracias...