Skip to content

Instantly share code, notes, and snippets.

@cezr
Last active January 16, 2017 19:44
Show Gist options
  • Save cezr/b01b76ce562c9c40bccf4b73d3aac176 to your computer and use it in GitHub Desktop.
Save cezr/b01b76ce562c9c40bccf4b73d3aac176 to your computer and use it in GitHub Desktop.
mailgun email validation API
//
// Mailgun Custom Address Validation API w/ v3
//
// Include in form submit:
//
// email_validator(email_address, event, {
// //api_key: ' api-key',
// in_progress: in_progress_callback, // called when request is made to validator
// success: success_callback, // called when validator has returned
// error: validation_error // called when an error reaching the validator has occured
// api_only: false
// });
//
// Jaanuu api_key 'pubkey-7q1gh73o94j24w77-gs8hzj73bngqoi9'
//
//
// Sample JSON in success callback:
//
// {
// "is_valid": true,
// "parts": {
// "local_part": "[email protected]",
// "domain": "example.com",
// "display_name": ""
// },
// "address": "[email protected]",
// "did_you_mean": null
// }
//
// More API details: https://api.mailgun.net/v3/address
//
function email_validator(address_text, event, opts) {
var options = {
api_only: false, // if used with another validation; use API only
in_progress: undefined, // called when request is made to validator
success: undefined, // called when validator has returned
error: undefined // called when an error reaching the validator has occured
};
options = $.extend(options, opts || {});
options.e = event;
// in progress
if (options && options.in_progress) {
options.in_progress(options.e);
}
if (options.api_only == false) {
var check = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
var error_message = false;
if (address_text.trim() == '')
error_message = 'Email address is required';
else if (address_text.length > 512)
error_message = 'Email address exceeds maxiumum allowable length of 512.';
else if (1 !== address_text.split('@').length-1)
error_message = 'Email address must contain only one @.';
else if (!check.test(address_text))
error_message = 'Please enter a valid email address.';
if (error_message) {
var data = {
address: address_text,
"is_valid": false,
error_mailgun: false,
error_message: error_message
};
if (options && options.error) {
options.error(data, options.e);
} else {
if (console) console.log(data, options.e);
}
return;
}
}
/*if (options && options.api_key == undefined) {
if (console) console.log('Please pass in api_key to email_validator.');
return;
}*/
$.ajax({
type: 'GET',
//url: 'https://api.mailgun.net/v2/address/validate?callback=?', // jsonp
url: 'https://api.mailgun.net/v3/address/validate?', // no jsonp
data: {
address: address_text,
api_key: 'pubkey-7q1gh73o94j24w77-gs8hzj73bngqoi9'
//api_key: options.api_key
},
// dataType: 'jsonp', // remove jsonp
//crossDomain: true,
success: function(data, textStatus) {
//console.log(data, textStatus);
data.error_mailgun = false;
if (options && options.success) {
if (data['is_valid'] == false)
data['error_message'] = 'Please enter a valid email address.';
options.success(data, options.e);
} else {
if (console) console.log(data, options.e);
}
},
error: function(xhr, textStatus, errorThrown){
if (console) console.log('email mailgun validation error', xhr,
textStatus, errorThrown);
var data = {
address: address_text,
"is_valid": false,
error_mailgun: true,
error_message: 'Error occurred, unable to validate address.'
};
if (options && options.error){
options.error(data, options.e);
} else {
if (console) console.log(data, options.e);
}
}
});
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment