Ver Mensaje Individual
  #25 (permalink)  
Antiguo 03/11/2008, 18:34
jesusjj
 
Fecha de Ingreso: noviembre-2007
Mensajes: 154
Antigüedad: 16 años, 5 meses
Puntos: 2
Pregunta Respuesta: Validar nif/cif

Buenas noches:
Estoy empezando con javascript a validar formularios. El tema es que tenía ya una función para validar el formulario, antes de enviarlo al fichero PHP receptor.

La duda, es que no tengo claro cómo utilizar este script dentro de mi función y el firebug me decía algo raro, del 'onblur'.

Les adjunto el código de la función que tengo para validar, por si me pueden echar una mano.

Código HTML:
function formValidaBuy() {
	var formulario2 = document.f_compra;
	var nombre = document.f_compra.nombre.value;
	var apellidos = document.f_compra.apellidos.value;
	var mail = document.f_compra.mail.value;
	var empresa = document.f_compra.empresa.value;
	var telefono = document.f_compra.telefono.value;
	var direccion = document.f_compra.direccion.value;
	var nif = document.f_compra.nif.value;
	var letras = ['T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E', 'T'];
		
	// se calculan los checkbox marcados

	var cont = 0;
	var total = formulario2.f_chk;
	
	for (var x=0; x < total.length; x++) {
		if (total[x].checked) {
			cont = cont + 1;	
		}
	}

//	alert ("Se han seleccionado " + cont + " programas")

	// se realizan las comprobaciones
	
	if(nombre.length == 0 || nombre == '' || /^\s+$/.test(nombre)) {
		alert("Tiene que escribir su nombre")
		nombre.focus();
		return false;
		
	} else if(apellidos.length == 0 || apellidos == '' || /^\s+$/.test(apellidos)) {
		alert("Tiene que escribir su/s apellido/s")
		apellidos.focus();
		return false;
		
	} else if(!(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test(mail))) {
		alert("Debe introducir una dirección de mail válida")
		mail.focus();		
		return false;
		
	} else if(empresa.length == 0 || empresa == '' || /^\s+$/.test(empresa)) {
		alert("Tiene que escribir el nombre de su empresa")
		empresa.focus();
		return false;

	} else if( nif == '' || nif.length == 0) {
		alert("Compruebe si ha introducido el NIF")
		nif.focus();
  	return false;
/*
	}else if (!(/^\d{8}[A-Z]$/.test(nif)) || (nif.charAt(8) != letras[(nif.substring(0, 8))%23]) || ) {
		alert("Compruebe si ha introducido el NIF correcto")
		nif.focus();
		return false;
*/
	} else if( !(/^\d{9}$/.test(telefono))) {
		alert("Compruebe que ha introducido correctamente el teléfono")
		telefono.focus();
		return false;
		
	} else if(cont < 1) {
		alert("Debe seleccionar al menos uno de los programas")
		return false;
	}
	
	alert("Muchas gracias por enviar el formulario")
	formulario2.submit();
}
Por otra parte, como pueden ver, tengo un for, para ver si se selecciona algún check y cuántos se han seleccionado, pero necesitaría saber si se puede hacer este 'conteo', con un getElementByType o algo así, porque debería cambiar el nombre del check de 'f_chk' a 'f_chk[]', para luego el tratamiento en PHP, o cambiar el nombre, sin que no se vuelva loco javascript.

El formulario es el siguiente:

Código HTML:
<p class="titulo">Compras</p>

<!-- <form id="compras" action="index.php?pag=9&contacto=envio&tipo=1" method="post" name="f_compra" onsubmit="return formValidaBuy(this);"> -->
<form id="compras" action="index.php?pag=9&contacto=envio&tipo=1" method="post" name="f_compra">	
	<fieldset>
    	<legend>Información de contacto</legend>
        <table align="center" width="90%">
        	<tr>
            	<td>
        			<label for="nombre">Nombre:</label>
        			<input type="text" id="nombre" name="nombre" /><br />
        		</td>
                <td>
                	<label for="empresa">Empresa:</label>
                    <input type="text" id="empresa" name="empresa" />
                </td>

			</tr>                
			<tr>
            	<td>              
                    <label for="apellidos">Apellidos:</label>
        			<input type="text" id="apellidos" name="apellidos" /><br />         				        	
				</td>
                <td>
                	<label for="nif">CIF / NIF:</label>
        			<input type="text" id="nif" name="nif" /><br />
                </td>
			</tr>
            <tr>
            	<td>                                    
			        <label for="mail">Email:</label>
			        <input type="text" id="mail" name="mail" />
				</td>
				<td>
	                <label for="direccion">Direccion:</label>
        			<input type="text" id="direccion" name="direccion" /><br />
                </td>
			</tr>                
			<tr>                
                <td colspan="2">
                	<label for="telefono">Teléfono:</label>
        			<input type="text" id="telefono" name="telefono" /><br />
                </td>
			</tr>
		</table>                                             
    </fieldset>
	<fieldset>
    	<legend>Productos</legend>
        <p>Seleccione los programas que le interesen:</p>
        <p>
          <input type="checkbox" value="cyewin" id="cyewin" name="f_chk" />&nbsp;CyEWin<br />
          <input type="checkbox" value="fyvwin" id="fyvwin" name="f_chk" />&nbsp;FyVWin<br />
          <input type="checkbox" value="nyswin" id="nyswin" name="f_chk" />&nbsp;NySWin<br />
        </p>
  </fieldset>
    
<!--    <input type="submit" value="enviar" /> -->
		<input type="button" onClick="javascript:formValidaBuy(this)" name="Submit" value="Enviar">
    <input type="reset" value="borrar" />
</form> 


Muchas gracias.