-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path468-Validate-Ip-Address.js
More file actions
76 lines (61 loc) · 1.73 KB
/
468-Validate-Ip-Address.js
File metadata and controls
76 lines (61 loc) · 1.73 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @param {string} IP
* @return {string}
*/
var validIPAddress = function(IP) {
if (IP.indexOf('.') !== -1) {
return validIPv4Address(IP) ? 'IPv4' : 'Neither';
} else if (IP.indexOf(':') !== -1) {
return validIPv6Address(IP) ? 'IPv6' : 'Neither';
}
return 'Neither';
};
var validIPv4Address = function(IP) {
const aNum = IP.split('.');
//檢查數量
if (aNum.length !== 4) {
return false;
}
for (let i = 0; i < aNum.length; i++) {
const v = aNum[i];
// 長度不能超過 3 必須大於 1,開頭為 0 禁止
if (v.length > 3 || v.length < 1 || (v.length !== 1 && v.charAt(0) === '0')) {
return false;
}
// 數字
for (let j = 0; j < v.length; j++) {
if ('0' > v.charAt(j) || v.charAt(j) > '9') {
return false;
}
}
// 不能超過 255 且大於 0
if (parseInt(v) > 255 || parseInt(v) < 0) {
return false;
}
}
return true;
};
var validIPv6Address = function(IP) {
const aNum = IP.split(':');
if (aNum.length !== 8) {
return false;
}
for (let i = 0; i < aNum.length; i++) {
let v = aNum[i].toLowerCase();
// 長度不能超過 4 且大於 1
if (v.length > 4 || v.length < 1) {
return false;
}
// 0-9a-f
for (let j = 0; j < v.length; j++) {
if ('0' > v.charAt(j) || (v.charAt(j) > '9' && v.charAt(j) < 'a') || v.charAt(j) > 'f') {
return false;
}
}
// 不能超過 65535 且大於 0
if (parseInt(v, 16) > 65535 || parseInt(v) < 0) {
return false;
}
}
return true;
};