Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148355
  • 博文数量: 22
  • 博客积分: 428
  • 博客等级: 下士
  • 技术积分: 281
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-14 14:28
文章分类

全部博文(22)

文章存档

2013年(7)

2012年(15)

分类: LINUX

2012-07-19 15:44:38


点击(此处)折叠或打开

  1. // check whether the str is a right IPv6 address
  2. function checkIPv6(str) {
  3.     var idx = str.indexOf("::");
  4.     // there is no "::" in the ip address
  5.     if (idx == -1) {
  6.         var items = str.split(":");
  7.         if (items.length != 8) {
  8.             return false;
  9.         } else {
  10.             for (i in items) {
  11.                 if (!isHex(items[i])) {
  12.                     return false;
  13.                 }
  14.             }
  15.             return true;
  16.         }
  17.     } else {
  18.         // at least, there are two "::" in the ip address
  19.         if (idx != str.lastIndexOf("::")) {
  20.             return false;
  21.         } else {
  22.             var items = str.split("::");
  23.             var items0 = items[0].split(":");
  24.             var items1 = items[1].split(":");
  25.             if ((items0.length + items1.length) > 7) {
  26.                 return false;
  27.             } else {
  28.                 for (i in items0) {
  29.                     if (!isHex(items0[i])) {
  30.                         return false;
  31.                     }
  32.                 }
  33.                 for (i in items1) {
  34.                     if (!isHex(items1[i])) {
  35.                         return false;
  36.                     }
  37.                 }
  38.                 return true;
  39.             }
  40.         }
  41.     }
  42. }

  43. // check whether every char of the str is a Hex char(0~9,a~f,A~F)
  44. function isHex(str) {
  45.     if(str.length == 0 || str.length > 4) {
  46.         return false;
  47.     }
  48.     str = str.toLowerCase();
  49.     var ch;
  50.     for(var i=0; i< str.length; i++) {
  51.         ch = str.charAt(i);
  52.         if(!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'f')) {
  53.             return false;
  54.         }
  55.     }
  56.     return true;
  57. }

阅读(3192) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~