$(function(){ $( 'form' ).submit( function( e ) { var form = $(this); if(form.find("[name='crm_contacto[nombre]']").val() == "") { alert("Debe ingresar su nombre completo"); form.find("[name='crm_contacto[nombre]']").focus(); return false; } if(form.find("[name='crm_contacto[telefono]']").val() == "") { alert("Debe ingresar su teléfono"); form.find("[name='crm_contacto[telefono]']").focus(); return false; } if(form.find("[name='crm_contacto[email]']").val() == "") { alert("Debe ingresar su correo"); form.find("[name='crm_contacto[email]']").focus(); return false; } if(!/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.([a-z]){2,4})$/.test( form.find("[name='crm_contacto[email]']").val() )) { alert("Debe ingresar un correo válido"); form.find("[name='crm_contacto[email]']").focus(); return false; } if(form.find("[name='crm_consultas[comentario]']").val() == "") { alert("Debe ingresar el mensaje"); form.find("[name='crm_consultas[comentario]']").focus(); return false; } form.find( '[type="submit"]' ).attr( 'disabled', 'disabled' ).html( 'Enviando...' ); $.post( '/?_ajax_=getDataContacto&action=save', form.formToJson( ), function( data ) { alert( 'Datos enviados.' ); form.find( '[type="submit"]' ).removeAttr( 'disabled' ).html( 'Enviar' ); }, 'jsonp' ); return false; }); $.fn.serializeArrayAll = function() { var rCRLF = /\r?\n/g; return this.map(function(){ //return this.elements ? jQuery.makeArray( this.elements ) : this; return jQuery.makeArray( this.elements ); }) .map(function( i, elem ){ var val = jQuery( this ).val(); if(elem.type == "checkbox") jQuery( this ).is(":checked") ? "" : val = ""; if(elem.type == "select-multiple") { if(jQuery( this ).hasClass("empty")) { return { name: elem.name, value: "" }; } } return val == null ? null : jQuery.isArray( val ) ? jQuery.map( val, function( val, i ){ return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) }; }) : { name: elem.name, value: val.replace( rCRLF, "\r\n" ) }; }).get(); } $.fn.formToJson = function () { a = $(this).serializeArrayAll();//$(this).serializeArray(); var o = {}; $.each(a, function() { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; } }) $(function(){ }) $(function(){ }) $(function(){ })