-
// check whether the str is a right IPv6 address
function checkIPv6(str) {
var idx = str.indexOf("::");
// there is no "::" in the ip address
if (idx == -1) {
var items = str.split(":");
if (items.length != 8) {
return false;
} else {
for (i in items) {
if (!isHex(items[i])) {
return false;
}
}
return true;
}
} else {
// at least, there are two "::" in the ip address
if (idx != str.lastIndexOf("::")) {
return false;
} else {
var items = str.split("::");
var items0 = items[0].split(":");
var items1 = items[1].split(":");
if ((items0.length + items1.length) > 7) {
return false;
} else {
for (i in items0) {
if (!isHex(items0[i])) {
return false;
}
}
for (i in items1) {
if (!isHex(items1[i])) {
return false;
}
}
return true;
}
}
}
}
// check whether every char of the str is a Hex char(0~9,a~f,A~F)
function isHex(str) {
if(str.length == 0 || str.length > 4) {
return false;
}
str = str.toLowerCase();
var ch;
for(var i=0; i< str.length; i++) {
ch = str.charAt(i);
if(!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'f')) {
return false;
}
}
return true;
}
- Jan 31 Thu 2013 11:56
(For javascript)ipv6 check format function
文章標籤
全站熱搜
留言列表
發表留言