点击(此处)折叠或打开
- // 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;
- }