/** * * @package SprintCMSv2_vitalspace.co.uk * @author Jim Tunstall * @copyright Copyright © 2009, Doctor Net Limited * * Newsletter signup * * Validate users data, post to local php script (to sidestep ajax cross domain issues) * and on success display message to user * * :note: break into functions? */ $(document).ready(function() { $("#subscribe").click(function() { // clear any previous errors $('.error').hide(); $('#newsletter').css('background','#4B08A1'); var bol_has_errors = false; // validate inputs // valudate name var str_name = $("input#str_name").val(); if ("" == str_name) { $('#str_name_error').html("Please enter your name"); $("#str_name_error").show(); bol_has_errors = true; } // validate email var str_email_regex = /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/; var str_email_address = $("#str_email_address").val(); if (("" == str_email_address) || (!str_email_address.match(str_email_regex))) { $('#str_email_address_error').html("Please enter a valid email address"); $("#str_email_address_error").show(); bol_has_errors = true; } // validate choice of mailing list var str_mailing_list = $('input[name=str_mailing_list]:checked').val(); switch(str_mailing_list) { case 'renting': // renting mailing list hash as provided by email campaigner str_mailing_list_hash = '2m2,d'; break; case 'buying': // buying mailing list hash as provided by email campaigner str_mailing_list_hash = '6n2,d'; break; default: $('#str_mailing_list_error').html("Please choose a mailing list"); $("#str_mailing_list_error").show(); bol_has_errors = true; break; } // was any of the user input data invalid if(bol_has_errors) { // alert the user by a background colour change and stop here (validation errors are already visible) $('#newsletter').css('background','red'); return false; } // user input data is valid, so create post data var str_subscribe_info = 'str_name=' + str_name + '&str_email_address=' + str_email_address + '&str_mailing_list_hash=' + str_mailing_list_hash; // post request to email campaigner, parse response and display to user $.ajax({ type: "POST", url: "/AjaxNewsLetter/signup", data: str_subscribe_info, success: function(xml) { $('#newsletter').html("

Request Sent

" + $(xml).find('message').text() + "

"); if($('response', xml).attr('success') == 'false') { $('#newsletter').css('background', 'red'); } }, error:function(xml){ $('#newsletter').html("

Error

You could not be added. Please try again later.

"); $('#newsletter').css('background','red'); } }); return false; }); });