Skip to content

Instantly share code, notes, and snippets.

@quis
Created March 7, 2011 17:51
Show Gist options
  • Save quis/858875 to your computer and use it in GitHub Desktop.
Save quis/858875 to your computer and use it in GitHub Desktop.
// Campaign Monitor jQuery plugin
// Chris @ Anywhichway 2010
//
// usage $(form).campaignMonitor(), where form is the signup form that Campaign Monitor generates
jQuery.fn.campaignMonitorSignup = function(options) {
var defaults = {
callbacks: {
success: function(){},
failure: function(){},
invalidEmail: function(){}
},
proxy: "" // URL to a PHP file on the same server. We use this to get around XSS
},
options = $.extend({}, defaults, options),
elements = {},
isValidEmail = function(emailFieldValue) {
// Validate email address with regex
var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return pattern.test(emailFieldValue);
};
return $(this).submit(
function(event) {
var url = this.action,
$form = $(this),
address = $(":text", $form).val();
if (!isValidEmail(address)) {
options.callbacks.invalidEmail();
return false;
}
$.post(
options.proxy,
{
url: url + "?" + $form.serialize()
},
function(data) {
if (data.search(/invalid/i) === -1) {
options.callbacks.success();
} else {
options.callbacks.failure();
}
},
"html"
);
return false;
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment