-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.js
57 lines (55 loc) · 2.41 KB
/
email.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
/**
* to validate email
* @param {string} email
*/
function isValidEmail(email) {
const pattern = {
emailReg: /^([^.][a-zA-Z0-9_\-\.+]+)[^.]@([\w][a-zA-Z0-9_\-\.+]+)\.([a-zA-Z]{2,5})$/,
errorAtRateReg: /(?=.*[@])/,
tldDotReg: /@([\w%*][a-zA-Z0-9_%*\-\.+]+)\.([a-zA-Z0-9]{0,5})$/,
tldTwoCharReg: /@[a-zA-Z0-9%*()&!<>{}]([a-zA-Z0-9%*()&!<>{}]+)((\.([a-zA-Z0-9]){2,5})+)$/,
canNotDotReg: /^[^.]/,
canNotSpecialReg: /^([^.][a-zA-Z0-9@_\-\.+]+)@([a-zA-Z0-9_%*\-\.+]+)\.([a-zA-Z0-9]{2,5})$/,
onlyCharDigReg: /^([^.][a-zA-Z0-9@_\-\.+]+)@([a-zA-Z0-9_\-\.+]+)\.([a-zA-Z0-9]{2,5})$/,
lastCharDotReg: /^([^.][a-zA-Z0-9@_\-\.+]+)[^.]@([\w][a-zA-Z0-9_\-\.+]+)\.([a-zA-Z0-9]{2,5})$/,
doubleAtRateReg: /^([^.][a-zA-Z0-9_\-\.+]+)[^.]@([\w][a-zA-Z0-9_\-\.+]+)\.([a-zA-Z0-9]{2,5})$/,
tldTwoCharSecReg: /^([^.][a-zA-Z0-9_\-\.+]+)[^.]@([\w][a-zA-Z0-9_\-\.+]+)\.([a-zA-Z]{2,5})$/
};
const errorMsgs = {
errorAtRate: "must contains “@” symbol",
tldDot: "tld can not start with dot “.”",
tldTwoChar: "last tld must contains at least two characters",
canNotDot: "email’s 1st character can not start with “.”",
canNotSpecial:
"email’s is only allow character, digit, underscore and dash",
onlyCharDig: "email’s tld is only allow character and digit",
lastCharDot: "email’s last character can not end with dot “.”",
doubleAtRate: "double @ is not allow",
tldTwoCharSec: "-email’s tld which has two characters can not contains digit",
generic: "Not valid email format"
};
if (pattern.emailReg.test(email)) {
return true;
} else if (!pattern.errorAtRateReg.test(email)) {
return errorMsgs.errorAtRate;
} else if (!pattern.tldDotReg.test(email)) {
return errorMsgs.tldDot;
} else if (!pattern.tldTwoCharReg.test(email)) {
return errorMsgs.tldTwoChar;
} else if (!pattern.canNotDotReg.test(email)) {
return errorMsgs.canNotDot;
} else if (!pattern.canNotSpecialReg.test(email)) {
return errorMsgs.canNotSpecial;
} else if (!pattern.onlyCharDigReg.test(email)) {
return errorMsgs.onlyCharDig;
} else if (!pattern.lastCharDotReg.test(email)) {
return errorMsgs.lastCharDot;
}else if(!pattern.doubleAtRateReg.test(email)){
return errorMsgs.doubleAtRate;
}else if(!pattern.tldTwoCharSecReg.test(email)){
return errorMsgs.tldTwoCharSec;
}else{
return errorMsgs.generic;
}
}
module.exports = isValidEmail;