var Validacion=function($){ var validaciones={ obligatorio: " es obligatorio", hora: " debe tener el formato hh:mm, por ejemplo: 11:30", nif: " no es un NIF o NIE válido", nie: " no es un NIE válido", cif: " no es un CIF válido" , nif_cif: " no es un NIF, CIF o NIE válido", email : " no es un email válido", numero: " debe ser un número entero, sin puntos ni comas", decimal: " debe ser un número con hasta dos decimales, utlizando el punto como separador", fecha : " debe tener el formato dd/mm/aaaa, por ejemplo 18/05/2014", color: " debe ser un color en hexadecimal, por ejemplo CC0000", iban: " debe ser un IBAN válido", aceptar: "debe estar marcado" } function nombreCampo(em){ em = $(em); var nombre=em.prop('title'); if(!nombre && em.parent().prop("tagName")=='LABEL' && em.parent().first()){ nombre=em.parent().first().text(); } if(!nombre && em.parent().prop("tagName")=='LABEL' && em.parent().last()){ nombre=em.parent().last().text(); } if(!nombre && em.prev() && ($(em.prev()).prop("tagName")=='LABEL' || $(em.prev()).prop("tagName")=='SPAN')){ nombre=em.prev().text(); } if(!nombre) nombre=em.prop('placeholder'); if(!nombre) nombre=em.prop('name'); if(!nombre) nombre=em.prop('id'); if(!nombre) nombre = ''; nombre=nombre.replace(":",""); return nombre; } $('.form-validacion').each(function(indice, el){ el.validar=function(event){ $(el).find('*').removeClass("campo_incorrecto"); var mensaje=""; for (valor in validaciones){ $(el).find('.'+valor).each(function(indice2, em){ if(($(em).prop("tagName")=="INPUT") || ($(em).prop("tagName")=="TEXTAREA") || ($(em).prop("tagName")=="SELECT")){ if($(em).prop('type') == 'checkbox') { var valor_campo=$(em).prop('checked'); } else{ var valor_campo=$(em).val(); } if(!FuncionesValidacion[valor+"Validar"](valor_campo)){ mensaje+="- El campo " + nombreCampo(em) + " "+validaciones[valor]+"
"; $(em).addClass("campo_incorrecto"); } } }); } if(mensaje){ alerta('Por favor, corrija los siguientes errores:
'+mensaje); try{ event.preventDefault() }catch(ex){} return false; } return true; } if($(el).hasClass('ajax')){ $(el).on('submit',function(event){ event.preventDefault(); if(this.validar()){ $(el).find('.btn_enviar').prop('disabled', true); $(el).find('.btn_enviar').fadeOut(function(){ $(el).find('.enviando-form').fadeIn(); }); jQuery.ajax({ url: $(el).prop('action'), method: $(el).prop('method'), data: $(el).serialize() }).done(function (response) { $( document ).trigger( "formAjaxDone" ); $(el).find('.btn_enviar').prop('disabled', false); $(el).find('.enviando-form').fadeOut(); $(el).find('.enviando-form').fadeOut(function(){ $(el).find('.btn_enviar').fadeIn(); }); $(el)[0].reset(); alerta(response); }).fail(function (response) { $(el).find('.enviando-form').fadeOut(); $(el).find('.enviando-form').fadeOut(function(){ $(el).find('.btn_enviar').fadeIn(); }); alerta(response); }); } }); /* el.addEvent('submit',function(event){ event.preventDefault() if(this.validar()){ this.send({onComplete: function(res){ alerta(res); }}); } });*/ }else{ if(!$(el).hasClass('sin_alerta')){ $(el).on('submit',el.validar); } } }); } var FuncionesValidacion = { nifValidar : function(nif){ if(nif=="") return true; cadena="TRWAGMYFPDXBNJZSQVHLCKE"; numeros="0123456789"; if (nif.length!=9){ return false; } /* for (i=0;i<8;i++) { numero = nif.charAt(i); if ( numeros.lastIndexOf(numero) == -1) return false; }*/ letra=nif.charAt(8).toUpperCase(); if ( cadena.lastIndexOf(letra) == -1) { return false; } letra_calculada = cadena.charAt((nif.substr(0,8) % 23)); if( letra_calculada != letra){ //return false; return this.nieValidar(nif); //compruebo si es un nie } return true; }, cifValidar : function (cif){ numeros="0123456789"; cadena="ABCDEFGHPQSKLMX"; cif=cif.toUpperCase(); if (cif.length!=9) return false; for (i=1;i<8;i++) { //comprueba si cif(i) no es un numero numero = cif.charAt(i); if ( numeros.lastIndexOf(numero) == -1 ) return false; } letra1=cif.charAt(0); letra9=cif.charAt(8); numero=cif.substr(1,8); if (letra1=="X") return false; if (cadena.indexOf(letra1) == -1) return false; control=0; for (i=0;i<7;i+=2){ aux=parseInt(numero.charAt(i))*2; control+=aux%10+(aux-aux%10)/10; } control=10-(control+parseInt(numero.charAt(1))+parseInt(numero.charAt(3))+parseInt(numero.charAt(5)))%10; if (control==10) control=0; if (( control==letra9 ) || ( letra9==String.fromCharCode(control+64) ) ) return true; else return false; }, nieValidar : function(nie){ if (nie.length!=9) return false; nie=nie.toUpperCase(); letra1=nie.charAt(0); letra9=nie.charAt(8); numero=nie.substr(1,8); switch(letra1){ case "X": nif = "0"+""+numero; return this.nifValidar(nif); break; case "Y": nif = "1"+""+numero; return this.nifValidar(nif); break; case "Z": nif = "2"+""+numero; return this.nifValidar(nif); break; default: return false; } }, nif_cifValidar : function(numero){ return (this.nifValidar(numero) || this.cifValidar(numero)); }, ibanValidar : function(valor){ return IBAN.isValid(valor); }, horaValidar : function(hora){ if ( hora == "" ) return true; var exp=new RegExp("(^([0-9]{2}):([0-9]{2})$)"); partes=hora.split(':'); if ( exp.test(hora) == false || partes[0]<0 || partes[0]>23 || partes[1]>59 || partes[1]<0 ){ return false; }else{ return true; } }, emailValidar : function(email){ if (email!='' && (email.indexOf("@")==-1 || email.indexOf(".")==-1 ) ){ return false; } return true; }, obligatorioValidar : function(valor){ if(valor==""){ return false; } return true; }, numeroValidar : function(valor){ if(Math.ceil(valor)==valor) return true; return false; }, decimalValidar : function(valor){ partes=valor.split('.'); if(partes.length>2) return false; if(partes[1] && partes[1].length>2) return false; if(Number(valor)==valor) return true; return false; }, fechaValidar : function(fecha){ if ( fecha == "" ){ return true; } var exp=new RegExp("(^([0-9]{2})/([0-9]{2})/([0-9]{4})$)"); partes=fecha.split('/'); partes[1]=parseInt(partes[1],10); switch (partes[1]) { case 1 : limite_dias= 31; break; case 3 : limite_dias= 31; break; case 5 : limite_dias= 31; break; case 7 : limite_dias= 31; break; case 8 : limite_dias= 31; break; case 10 : limite_dias= 31; break; case 12 : limite_dias= 31; break; case 4 : limite_dias= 30; break; case 6 : limite_dias= 30; break; case 9 : limite_dias= 30; break; case 11 : limite_dias= 30; break; case 2: if (partes[2]%400==0) limite_dias=29 else if (partes[2]%100==0) limite_dias=28 else if (partes[2]%4==0) limite_dias=29 else limite_dias=28; break; default : limite_dias=0; } if (exp.test(fecha) == false || partes[0]>limite_dias || partes[0]<1 || partes[1]<1 || partes[1]>12){ return false; } return true; }, colorValidar: function(color){ if ( color == "" ) return true; var exp=new RegExp("^[0-9a-fA-F]{6}$"); return exp.test(color); }, aceptarValidar:function(valor){ return valor; } } jQuery( document ).ready(function(jQuery){Validacion(jQuery)});