jQuery(document).on('ready', function(){ NL.onReady(); }); var NL = { onReady : function(){ jQuery(document).on('submit', '.newsletter-submit', NL.initNewsletter); jQuery(document).on('click', '.newsletter-submit', NL.initNewsletter); }, initNewsletter : function(){ NL.context = jQuery(this).closest('form'); NL.btn = jQuery(this); NL.lang = jQuery('html').attr('lang'); NL.email = jQuery('input#email', NL.context).val(); NL.error = false; NL.btn.prop('disabled', true); jQuery('input', NL.context).removeClass('error'); new NL.validate(); if(NL.error === false) NL.sendForm(); NL.btn.prop('disabled', false); return false; }, validate : function(){ if(!(NL.validateEmail())){ jQuery('input#email', NL.context).addClass('error'); NL.error = true; } }, validateEmail : function(){ var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); return pattern.test(NL.email); }, sendForm : function(){ var values = jQuery(NL.context).serialize(); jQuery.ajax({ url: crSN.ajaxurl, type:'POST', data: "action=sn_send_newsletter&" + values, success: function(response){ console.log(response); NL.context.prev().remove(); jQuery('.msg.newsletter').remove(); NL.context.before('
'); NL.context.hide(); NL.context.next().hide(); NL.context.prev().fadeIn(); }, error : function(jqXHR, textStatus, errorThrown){ jQuery(NL.btn).after(''+errorThrown+''); } }); } }