Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/08/2008, 15:33
perris
 
Fecha de Ingreso: enero-2008
Mensajes: 73
Antigüedad: 16 años, 4 meses
Puntos: 0
ejecutar un codigo javascrit dentro de un html o php

lo que quiero es que me valide los campos del formulario pero que el codigo sea un script aparte. este es el formulario

Código HTML:
<html>
<head><title>ESTA ES UNA PRUEBA</title><B><CENTER>APLICACION WEB</B></CENTER><head>
<BODY>
<div id="capa1" style="position:absolute; left:350px; top:250px; width:150px; height:100px">
<form name="formulario" onSubmit="return valida(formulario)" method="post" action="proceso.php" >
  
<table border=2>
<tr>
	<td>Identificacion</td>
	<td><input type="text" name="a1"></td>
</tr>

<tr>
	<td>Nombre</td>
	<td><input type="text" name="a2"></td>
</tr>

<tr>
	<td>Apellido</td>
	<td><input type="text" name="a3"></td>
</tr>

<tr>
	<td>Telefono</td>
	<td><input type="text" name="a4"></td>
</tr>

<tr>
	<td>Direccion</td>
	<td><input type="text" name="a5"></td>
</tr>

</table>

<div id="capa2" style="position:absolute"; left:455px; top:600px; width:200px; heigh:20px">
<input type="submit" name="submit"  value="enviar">
</div>
</form>
</div>
</BODY>
</html> 
el action ejecuta un .php que es el encargado de guardar:

[PHP][<?php
include("conex.php");
$link=conectarse();
$sql = "insert into clientes (id_cli, nom_cli, ape_cli,tel_cli, dir_cli)
values('$a1','$a2','$a3','$a4', '$a5')";
$result = mysql_query($sql);
echo "gracias hemos recibido sus datos";
?>/PHP]

este el codigo javascript que quiero que se ejecute llamandolo desde alguna parte del codigo html para validar se llama validar.js
[PHP][<script>
function valida(formulario){

er=/^[0-9]+$/;
if(er.test(formulario.a1.value)==false){
alert("El campo identidad es obligatorio y de tipo numerico");
return false; // NO se enviará el formulario.
}
er=/^[a-zA-Z ñÑáéíóúÁÉÍÓÚuüÜÇç]+$/;
if(er.test(formulario.a2.value)==false){
alert("El campo nombre es obligatorio y de tipo texto");
return false; // NO se enviará el formulario.
}
er=/^[a-zA-Z ñÑáéíóúÁÉÍÓÚuüÜÇç]+$/;
if(er.test(formulario.a3.value)==false){
alert("El campo apellidos es obligatorio y de tipo texto");
return false; // NO se enviará el formulario.
}
er=/^[0-9]+$/;
if(er.test(formulario.a4.value)==false){
alert("El campo telefono es obligatorio y numerico");
return false; // NO se enviará el formulario.
}
// formato aceptado: [email protected]_p4.com

er=/^[a-zA-Z ñÑáéíóúÁÉÍÓÚuüÜÇç]+$/;
if(er.test(formulario.a5.value)==false){
alert("El campo telefono es obligatorio y numerico");
return true; // NO se enviará el formulario.
}


return true; // Se enviará el formulario.
}
</script>/PHP]

gracias por la ayuda