Last active
August 29, 2015 13:56
-
-
Save sabaraouf/4174be41e111df83855d to your computer and use it in GitHub Desktop.
Validate and submit mailchimp form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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