Skip to content

Instantly share code, notes, and snippets.

@sabaraouf
Last active August 29, 2015 13:56
Show Gist options
  • Save sabaraouf/4174be41e111df83855d to your computer and use it in GitHub Desktop.
Save sabaraouf/4174be41e111df83855d to your computer and use it in GitHub Desktop.
Validate and submit mailchimp form
$(document).ready(function() {
$('#subscribe').submit(function() {
if (!valid_email_address($("#email").val()))
{
$(".message").html('The email address you entered was invalid. Please make sure you enter a valid email address to subscribe.');
}
else
{
$(".message").html("<span style='color:green;'>Adding your email address...</span>");
$.ajax({
url: 'subscribe.php',
data: $('#subscribe').serialize(),
type: 'POST',
success: function(msg) {
if(msg=="success")
{
$("#email").val("");
$(".message").html('<span style="color:green;">You have successfully subscribed to our mailing list.</span>');
}
else
{
$(".message").html('The email address you entered was invalid. Please make sure you enter a valid email address to subscribe.');
}
}
});
}
return false;
});
});
function valid_email_address(email)
{
var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
return pattern.test(email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment