-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatorEmail.js
65 lines (56 loc) · 1.53 KB
/
ValidatorEmail.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
import Validator from './Validator';
export default class ValidatorEmail extends Validator {
constructor(defaultMessage) {
super(defaultMessage);
}
valid(message) {
const regexEmail = /^(([^<>()\[\]\\.,;:\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,}))$/;
this.addStep(
'email',
v => {
return regexEmail.test(String(v).toLowerCase());
},
message
);
return this;
}
onlyFrom(domains, message) {
var domainValue = normalizeDomains(domains);
this.addStep(
'onlyFrom',
function(v) {
let domainListLength = domainValue.length;
for (; domainListLength--; ) {
if (v.toLowerCase().endsWith(domainValue[domainListLength].toLowerCase())) return true;
}
return false;
},
message
);
return this;
}
notFrom(domains, message) {
var domainValue = normalizeDomains(domains);
this.addStep(
'notFrom',
function(v) {
let domainListLength = domainValue.length;
for (; domainListLength--; ) {
if (v.toLowerCase().endsWith(domainValue[domainListLength].toLowerCase())) return false;
}
return true;
},
message
);
return this;
}
}
function normalizeDomains(domains){
if (typeof domains === 'string' && domains.indexOf(',') !== -1) {
return domains.split(',');
} else if (domains instanceof Array) {
return domains;
} else {
return [domains];
}
}