$(document).ready(function() { $("#nom").focus(); $('#envia').click(function() { envia=true; $(".oblig_dades").each(function (index) { id="#"+$(this).attr('id'); $(id).css('border-color', '#ccc'); if ($(this).val()=="") { //$(id).attr('type', 'error'); $(id).css('border-color', 'red'); envia=false; } }); if (envia==true) { if (!validar_email($("#email").val())) { envia=false; $("#email").css('border-color', 'red'); } } if (envia==true) { if ($("#tmptxt").val()!='') { envia=false; $("#tmptxt").css('border-color', 'blue'); } } if (envia==true) { var parametros = { "lang" : '', "nom" : $("#nom").val(), "email" : $("#email").val(), "missatge" : $("#missatge").val() }; $.ajax({ data: parametros, url: 'envia_email.php', type: 'get', beforeSend: function(){ $("#boto").html(''); }, success: function (response) { $("#boto").html(response); } }); } }); function validar_email(valor) { // creamos nuestra regla con expresiones regulares. var filter = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/; // utilizamos test para comprobar si el parametro valor cumple la regla if(filter.test(valor)) return true; else return false; } });