-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathemail.validator.util.ts
171 lines (131 loc) · 4.08 KB
/
email.validator.util.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* a-z = 97 - 122
* 0-9 = 48 - 57
* 95 = _
* 45 = -
* 64 = @
* 46 = .
*/
const ValidChars = { min: 97, max: 122, specials: [95, 45, 64, 46, 43] };
const ValidCharsNum = { min: 48, max: 57 };
export const GetCharCode = (char: string): number => {
return typeof char === 'string' ? char.charCodeAt(0) : 0;
};
export const IsSpecialChar = (char: string): boolean => {
const code = GetCharCode(char);
return ValidChars.specials.includes(code);
};
export const IsAlphabet = (char: string): boolean => {
const code = GetCharCode(char);
const isGreaterThanMin = code >= ValidChars.min;
const isLessThanMax = code <= ValidChars.max;
return isGreaterThanMin && isLessThanMax;
};
export const IsNumber = (char: string): boolean => {
const charCode = GetCharCode(char);
const isValid =
charCode >= ValidCharsNum.min && charCode <= ValidCharsNum.max;
return isValid;
};
export const IsValidChar = (char: string): boolean => {
const isAlphabet = IsAlphabet(char);
const isSpecial = IsSpecialChar(char);
const isNumber = IsNumber(char);
return isAlphabet || isSpecial || isNumber;
};
export const HasValidLength = (email: string): boolean => {
return typeof email === 'string' && email.length <= 256;
};
export const IsValidPart = (part: string): boolean => {
const hifen = 45;
const isValidSpecialChar = (char: string): boolean =>
GetCharCode(char) === hifen;
const chars = part.replace(/\./g, '').split('');
let i = 0;
while (chars[i]) {
const isValidChar =
IsAlphabet(chars[i]) ||
IsNumber(chars[i]) ||
isValidSpecialChar(chars[i]);
if (!isValidChar) return false;
i++;
}
return true;
};
export const IsValidOrganizationName = (name: string): boolean => {
return name.length <= 10 && name.length >= 2;
};
export const IsValidCountry = (country: string): boolean => {
const isValidLength = country.length > 0 && country.length <= 4;
if (!isValidLength) return false;
const chars = country.split('');
let i = 0;
while (chars[i]) {
const isChar = IsAlphabet(chars[i]);
if (!isChar) return false;
i++;
}
return true;
};
export const IsValidNick = (email: string): boolean => {
const nick = email.split('@')?.[0];
return (
!nick.startsWith('+') &&
!nick.endsWith('+') &&
!nick.startsWith('-') &&
!nick.endsWith('-')
);
};
export const IsValidDomain = (email: string): boolean => {
const domain = email.split('@');
const parts = domain[1].split('.');
if (parts.length === 1 || parts.length > 3) return false;
const isInValidStartAndEnd = domain[1].startsWith('-') ||
domain[1].endsWith('-');
if (isInValidStartAndEnd) return false;
const isLessThanMax = email.length <= 64;
if (!isLessThanMax) return false;
const isValidOrgName = IsValidOrganizationName(parts[1]);
if (typeof parts[2] === 'string') {
const isValidCountry = IsValidCountry(parts[2]);
if (!isValidCountry) return false;
}
if (!isValidOrgName) return false;
let i = 0;
while (parts[i]) {
const isValidPart = IsValidPart(parts[i]);
if (!isValidPart) return false;
i++;
}
return true;
};
/**
* @description validate email
* @param email string with @ symbol
* @returns true if is a valid email and false if not
*/
export const IsValidEmail = (email: string): boolean => {
const isString: boolean = typeof email === 'string';
if (!isString) return false;
const trimEmail = email.trim().toLowerCase();
const isValidFirsChar: boolean = IsAlphabet(trimEmail[0]) ||
IsNumber(trimEmail[0]);
if (!isValidFirsChar) return false;
const hasOnlyOneAt: boolean = trimEmail.split('')
.filter((char) => char === '@').length === 1;
const hasOnlyOnePlus: boolean = trimEmail.split('')
.filter((char) => char === '+').length > 1;
if (!hasOnlyOneAt || hasOnlyOnePlus) return false;
const isValidLength = HasValidLength(trimEmail);
if (!isValidLength) return false;
const isValidDomain = IsValidDomain(trimEmail);
if (!isValidDomain) return false;
if (!IsValidNick(trimEmail)) return false;
const hasInvalidChar = trimEmail
.split('')
.map((char): boolean => IsValidChar(char))
.includes(false);
if (hasInvalidChar) return false;
return true;
};
export default IsValidEmail;