-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (46 loc) · 1.59 KB
/
index.js
File metadata and controls
56 lines (46 loc) · 1.59 KB
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
"use strict";
/**
* Jaiminho library features.
*
* @author João Eduardo Montandon
*/
const dns = require('dns');
class Jaimito {
/**
* check e-mail validity
*
* @param {String} email email that will be checked
* @param {Function} callback a callback that can be used instead of Promise
*/
static validate(email, callback) {
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
callback = callback || function() { };
return new Promise((resolve, reject) => {
let emailDomain = email.split('@')[1];
const options = {
family: 4,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
all: true
};
if(!regex.test(email)) {
resolve(false);
return callback(null, false);
}
else {
dns.resolve(emailDomain, 'MX', (err, records) => {
if(!err && records.length > 0) {
resolve(true);
return callback(null, true);
} else if((err && err.code === "ENOTFOUND") || (records && records.length === 0)) {
resolve(false);
return callback(null, false);
} else {
reject(err);
return callback(err);
}
});
}
});
}
}
module.exports = Jaimito;