forked from mailgun/validator-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailgun_validator.js
144 lines (129 loc) · 4.69 KB
/
mailgun_validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Mailgun Address Validation Plugin
//
// Attaching to a form:
//
// $('jquery_selector').mailgun_validator({
// 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
// });
//
//
// 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/v2/address
//
(function( $ ) {
$.fn.mailgun_validator = function(options) {
return this.each(function() {
var thisElement = $(this);
thisElement.focusout(function(e) {
//Trim string and autocorrect whitespace issues
var elementValue = thisElement.val();
elementValue = $.trim(elementValue);
thisElement.val(elementValue);
//Attach event to options
options.e = e;
run_validator(elementValue, options, thisElement);
});
});
};
function run_validator(address_text, options, element) {
//Abort existing AJAX Request to prevent flooding
if(element.mailgunRequest) {
element.mailgunRequest.abort();
element.mailgunRequest = null;
}
// don't run validator without input
if (!address_text) {
return;
}
// validator is in progress
if (options && options.in_progress) {
options.in_progress(options.e);
}
// don't run dupicate calls
if (element.mailgunLastSuccessReturn) {
if (address_text == element.mailgunLastSuccessReturn.address) {
if (options && options.success) {
options.success(element.mailgunLastSuccessReturn, options.e);
}
return;
}
}
// length and @ syntax check
var error_message = false;
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 @.';
if (error_message) {
if (options && options.error) {
options.error(error_message, options.e);
}
else {
if (console) console.log(error_message);
}
return;
}
// require api key
if (options && options.api_key == undefined) {
if (console) console.log('Please pass in api_key to mailgun_validator.');
}
// timeout incase of some kind of internal server error
var timeoutID = setTimeout(function() {
error_message = 'Error occurred, unable to validate address.';
if (!success) {
//Abort existing AJAX Request for a true timeout
if(element.mailgunRequest) {
element.mailgunRequest.abort();
element.mailgunRequest = null;
}
if (options && options.error) {
options.error(error_message, options.e);
}
else {
if (console) console.log(error_message);
}
}
}, 30000); //30 seconds
// make ajax call to get validation results
element.mailgunRequest = $.ajax({
type: "GET",
url: 'https://api.mailgun.net/v2/address/validate?callback=?',
data: { address: address_text, api_key: options.api_key },
dataType: "jsonp",
crossDomain: true,
success: function(data, status_text) {
clearTimeout(timeoutID);
element.mailgunLastSuccessReturn = data;
if (options && options.success) {
options.success(data, options.e);
}
},
error: function(request, status_text, error) {
clearTimeout(timeoutID);
error_message = 'Error occurred, unable to validate address.';
if (options && options.error) {
options.error(error_message, options.e);
}
else {
if (console) console.log(error_message);
}
}
});
}
})( jQuery );