1// 2// Mailgun Address Validation Plugin 3// 4// Attaching to a form: 5// 6// $('jquery_selector').mailgun_validator({ 7// api_key: 'api-key', 8// in_progress: in_progress_callback, // called when request is made to validator 9// success: success_callback, // called when validator has returned 10// error: validation_error, // called when an error reaching the validator has occured 11// }); 12// 13// 14// Sample JSON in success callback: 15// 16// { 17// "is_valid": true, 18// "parts": { 19// "local_part": "[email protected]", 20// "domain": "example.com", 21// "display_name": "" 22// }, 23// "address": "[email protected]", 24// "did_you_mean": null 25// } 26// 27// More API details: https://api.mailgun.net/v2/address 28// 29 30(function ($) { 31 $.fn.mailgun_validator = function (options) { 32 return this.each(function () { 33 var thisElement = $(this); 34 thisElement.focusout(function (e) { 35 //Trim string and autocorrect whitespace issues 36 var elementValue = thisElement.val(); 37 elementValue = $.trim(elementValue); 38 thisElement.val(elementValue); 39 40 //Attach event to options 41 options.e = e; 42 43 run_validator(elementValue, options, thisElement); 44 }); 45 }); 46 }; 47 48 function run_validator(address_text, options, element) { 49 //Abort existing AJAX Request to prevent flooding 50 if (element.mailgunRequest) { 51 element.mailgunRequest.abort(); 52 element.mailgunRequest = null; 53 } 54 55 // don't run validator without input 56 if (!address_text) { 57 return; 58 } 59 60 // validator is in progress 61 if (options && options.in_progress) { 62 options.in_progress(options.e); 63 } 64 65 // don't run dupicate calls 66 if (element.mailgunLastSuccessReturn) { 67 if (address_text == element.mailgunLastSuccessReturn.address) { 68 if (options && options.success) { 69 options.success(element.mailgunLastSuccessReturn, options.e); 70 } 71 return; 72 } 73 } 74 75 // length and @ syntax check 76 var error_message = false; 77 if (address_text.length > 512) 78 error_message = 'Email address exceeds maxiumum allowable length of 512.'; 79 else if (1 !== address_text.split('@').length - 1) 80 error_message = 'Email address must contain only one @.'; 81 82 if (error_message) { 83 if (options && options.error) { 84 options.error(error_message, options.e); 85 } 86 else { 87 if (console) console.log(error_message); 88 } 89 return; 90 } 91 92 // require api key 93 if (options && options.api_key == undefined) { 94 if (console) console.log('Please pass in api_key to mailgun_validator.'); 95 } 96 97 // timeout incase of some kind of internal server error 98 var timeoutID = setTimeout(function () { 99 error_message = 'Error occurred, unable to validate address.'; 100 if (!success) { 101 //Abort existing AJAX Request for a true timeout 102 if (element.mailgunRequest) { 103 element.mailgunRequest.abort(); 104 element.mailgunRequest = null; 105 } 106 107 if (options && options.error) { 108 options.error(error_message, options.e); 109 } 110 else { 111 if (console) console.log(error_message); 112 } 113 } 114 }, 30000); //30 seconds 115 116 // make ajax call to get validation results 117 element.mailgunRequest = $.ajax({ 118 type: "GET", 119 url: 'https://api.mailgun.net/v2/address/validate?callback=?', 120 data: { address: address_text, api_key: options.api_key }, 121 dataType: "jsonp", 122 crossDomain: true, 123 success: function (data, status_text) { 124 clearTimeout(timeoutID); 125 126 element.mailgunLastSuccessReturn = data; 127 if (options && options.success) { 128 options.success(data, options.e); 129 } 130 }, 131 error: function (request, status_text, error) { 132 clearTimeout(timeoutID); 133 error_message = 'Error occurred, unable to validate address.'; 134 135 if (options && options.error) { 136 options.error(error_message, options.e); 137 } 138 else { 139 if (console) console.log(error_message); 140 } 141 } 142 }); 143 } 144})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.