<转>常用的js验证

常用的js验证

  1. //添加时间侦听
  2. function addListener(e, eventType, listener)  
  3. {  
  4.     Event.observe(e, eventType, listener);  
  5. };  
  6.   
  7. //是否零长度字符串  
  8. function IsNone(s)  
  9. {  
  10.     return s.toString().empty();  
  11. }  
  12.   
  13. //是否只有空白字符  
  14. function IsEmpty(s)  
  15. {  
  16.     return s.toString().blank();  
  17. }  
  18.   
  19. //是否是数字  
  20. function IsNumber(s)  
  21. {  
  22.     s = s.toString();  
  23.     if (s.empty() || s.blank())  
  24.         return false;  
  25.     return !isNaN(s);  
  26. }  
  27.   
  28. //是否是整数  
  29. function IsInteger(s)  
  30. {  
  31.     if (!IsNumber(s))  
  32.         return false;  
  33.     s = (s - 0).toString();  
  34.     return s.match(/^[-]?/d{1,15}$/gi) != null//整型精度为15位。  
  35. }  
  36.   
  37. //是否是双精度型  
  38. function IsDouble(s)  
  39. {  
  40.     if (!IsNumber(s))  
  41.         return false;  
  42.     s = (s - 0).toString();  
  43.     if (s.match(/^[-]?(/d+)[.]?(/d+)$/gi) == null)  
  44.         return false;  
  45.     return RegExp.$1.length + RegExp.$2.length <= 17; //浮点型精度为17位。  
  46. }  
  47.   
  48. function IsDate(s)  
  49. {  
  50.     var reg = /^((((1[6-9]|[2-9]/d)/d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]/d|3[01]))|(((1[6-9]|[2-9]/d)/d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]/d|30))|(((1[6-9]|[2-9]/d)/d{2})-0?2-(0?[1-9]|1/d|2[0-8]))|(((1[6-9]|[2-9]/d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;  
  51.     return reg.test(s);  
  52. }  
  53.   
  54. function IsTime(str)  
  55. {  
  56.     var tmp = str.toString();  
  57.     return (tmp.match(/^([01]?[0-9]|2[0-3]):([012345]?[0-9]|5[1-9]):([012345]?[0-9]|5[1-9])$/g) != null)  
  58. }  
  59.   
  60.   
  61. //功能:常用正则表达式  
  62. function isEmail(str)  
  63. {  
  64.     return str.toString().match(/^(?:[;]?[/w_-]+(?:[.][/w_-]+)*@[/w_-]+(?:[.][/w_-]+)+)+$/gi) != null;  
  65. }  
  66.   
  67. function isQQ(str)  
  68. {  
  69.     return str.toString().match(/^[1-9]/d{4,12}$/g) != null;  
  70. }  
  71.   
  72. //xxxx-xxxxxxxx-xxxx  
  73. function isPhone(str)  
  74. {  
  75.     return str.toString().match(/^((/(/d{2,3}/))|(/d{3}/-))?(/(0/d{2,3}/)|0/d{2,3}-)?[1-9]/d{6,7}(/-/d{1,4})?$/g) != null;  
  76. }  
  77.   
  78.     //1xxxxxxxxxx  
  79. function isMobile(str)  
  80. {  
  81.     return str.toString().match(/^((/(/d{2,3}/))|(/d{3}/-))?1/d{10}$/g) != null;  
  82. }  
  83.   
  84. //1xxxxxxxxxxx,xxxx-xxxxxxxx-xxxx,...  
  85. //"^(()|())(,(()|()))*$"  
  86. function isPhoneOrMobileList(str)  
  87. {  
  88.     return str.toString().match(/^((((/(/d{2,3}/))|(/d{3}/-))?1/d{10})|(((/(/d{2,3}/))|(/d{3}/-))?(/(0/d{2,3}/)|0/d{2,3}-)?[1-9]/d{6,7}(/-/d{1,4})?))(,((((/(/d{2,3}/))|(/d{3}/-))?1/d{10})|(((/(/d{2,3}/))|(/d{3}/-))?(/(0/d{2,3}/)|0/d{2,3}-)?[1-9]/d{6,7}(/-/d{1,4})?)))*$/g) != null;  
  89. }  
  90.   
  91. //xx@xx.xx,xx@xx.xx,...  
  92. function isEmailList(emails)  
  93. {  
  94.     return emails.all(function(email) { return isEmail(email.strip()); });  
  95. }  
  96.   
  97. //A-Z,a~z,0~9,",","-"  
  98. function isAscii(str)  
  99. {  
  100.     return str.toString().match(/^[A-Za-z0-9,-]*$/gi) != null;  
  101. }  
  102.   
  103. //http:// or https://  
  104. function isUrl(str)  
  105. {  
  106.     return str.toString().match(/^(http|https):////[A-Za-z0-9]+/.[A-Za-z0-9]+[//=/?%/-&_~`@[/]/':+!]*([^/"/"])*$/gi) != null;  
  107. }  
  108.   
  109. function isFtp(str)  
  110. {  
  111.     return str.toString().match(/^ftp:////[A-Za-z0-9]+/.[A-Za-z0-9]+[//=/?%/-&_~`@[/]/':+!]*([^/"/"])*$/gi) != null;  
  112. }  
  113.   
  114. function isPostCode(str)  
  115. {  
  116.     return str.toString().match(/^[0-9]{6}$/g) != null;  
  117. }  
  118.   
  119. //检测有无重复邮箱名  
  120. function checkEmailsReteration(emails)  
  121. {  
  122.     return emails.length > emails.map(function(email) { return email.strip().toLowerCase(); }).uniq().length;  
  123. }  
  124.   
  125. //功能:页面展示时,处理文章编辑器中插入的图片的尺寸,使之适合页面大小,避免破坏页面布局  
  126. function ResizeImage(handleObj,w,h)  
  127. {  
  128.     var coll = handleObj.select("img");  
  129.     for(var i = 0; i < coll.length; i++)  
  130.     {  
  131.         var iWidth = coll[i].width;  
  132.         var iHeight = coll[i].height;  
  133.         if(coll[i].style.width != "")  
  134.             iWidth = parseInt(coll[i].style.width);  
  135.         if(coll[i].style.height != "")  
  136.             iHeight = parseInt(coll[i].style.height);  
  137.         if(iWidth >= iHeight)  
  138.         {  
  139.             if(iWidth> w)  
  140.             {  
  141.                 if(coll[i].style.width != "")  
  142.                 {  
  143.                     coll[i].style.width = w;  
  144.                     coll[i].style.height = (w * iHeight)/ iWidth;  
  145.                 }  
  146.                 else  
  147.                 {  
  148.                     coll[i].width = w;  
  149.                     coll[i].height = (w * iHeight)/ iWidth;  
  150.                 }  
  151.             }  
  152.         }  
  153.         else  
  154.         {  
  155.             if(iHeight > h)  
  156.             {  
  157.                 if(coll[i].style.width != "")  
  158.                 {  
  159.                     coll[i].style.width = (h * iWidth)/ iHeight;  
  160.                     coll[i].style.height = h;  
  161.                 }  
  162.                 else  
  163.                 {  
  164.                     coll[i].height = h;  
  165.                     coll[i].width = (h * iWidth)/ iHeight;  
  166.                 }  
  167.             }  
  168.         }  
  169.     }  
  170. }  
  171.   
  172. function ResizeImageInWidth(handleObj,w,h)  
  173. {  
  174.     var coll = handleObj.select("img");  
  175.     var newW = 0, newH = 0;  
  176.   
  177.     for(var i = 0; i < coll.length; i++)  
  178.     {  
  179.         var iWidth = coll[i].width;  
  180.         var iHeight = coll[i].height;  
  181.         if(coll[i].style.width != "")  
  182.             iWidth = parseInt(coll[i].style.width);  
  183.         if(coll[i].style.height != "")  
  184.             iHeight = parseInt(coll[i].style.height);  
  185.         if(iWidth >= iHeight)  
  186.         {  
  187.             if(iWidth> w)  
  188.             {  
  189.                 newW = w;  
  190.                 newH = (w * iHeight)/ iWidth;  
  191.             }  
  192.         }  
  193.         else  
  194.         {  
  195.             if(iWidth> w)  
  196.             {  
  197.                 newW = w;  
  198.                 newH = (w * iHeight)/ iWidth;  
  199.                 if(newH > (1.5*h))  
  200.                     newH = 1.5*h;  
  201.             }  
  202.             else if(iHeight > (1.5*h))  
  203.             {  
  204.                 newW = iWidth;  
  205.                 newH = 1.5*h;  
  206.             }  
  207.         }  
  208.         if(newW > 0)  
  209.         {  
  210.             if(coll[i].style.width != "")  
  211.             {  
  212.                 coll[i].style.width = newW;  
  213.                 coll[i].style.height = newH;  
  214.             }  
  215.             else  
  216.             {  
  217.                 coll[i].width = newW;  
  218.                 coll[i].height = newH;  
  219.             }  
  220.             newW = 0;  
  221.             newH = 0;  
  222.         }  
  223.     }  
  224. }  
  225.   
  226. function resizeImageByWidth(o, width)  
  227. {  
  228.     var oldWidth = 0;  
  229.     var oldHeight = 0;  
  230.   
  231.     if(o != null && width > 0)  
  232.     {  
  233.         oldWidth = parseInt(o.width);  
  234.         oldHeight = parseInt(o.height);  
  235.         if(oldWidth > 0 && oldHeight > 0)  
  236.         {  
  237.             var z = width / oldWidth;  
  238.             var height = parseInt(oldHeight * z);  
  239.             o.width = width;  
  240.             o.height = height;  
  241.         }  
  242.     }  
  243. }  
  244.   
  245. function resizeImageByWidthEx(srcWidth, srcHeight, setWidth){  
  246.         return (srcWidth > 0 && srcHeight > 0 && srcWidth > setWidth) ? {width: setWidth, height: srcHeight * (setWidth / srcWidth)} : {width: srcWidth, height: srcHeight};  
  247. }  
  248.   
  249. function SetSameHeight()  
  250. {  
  251.     var divLeft = $("divDoorLeft");  
  252.     var divMiddle = $("divDoorMiddle");  
  253.     var divRight = $("divDoorRight");  
  254.     var divMain = $("divDoorMain");  
  255.     if(divLeft!=null)  
  256.     {  
  257.         var maxHeight = 0;  
  258.         if(divMain != null)  
  259.         {  
  260.             maxHeight = Math.max(divLeft.scrollHeight, divMain.scrollHeight);  
  261.             divLeft.style.height = maxHeight-25 + 'px';  
  262.             divMain.style.height = maxHeight + 'px';  
  263.         }  
  264.         else  
  265.         {  
  266.             maxHeight = Math.max(divLeft.scrollHeight,divMiddle.scrollHeight, divRight.scrollHeight);  
  267.             divLeft.style.height = maxHeight-25 + 'px';  
  268.             if(divMiddle.scrollHeight < maxHeight)  
  269.                 divMiddle.style.height = maxHeight + 'px';  
  270.             divRight.style.height = maxHeight-25 + 'px';  
  271.         }  
  272.     }  
  273. }  
  274.   
  275. function SetLate()  
  276. {  
  277.     SetSameHeight.delay(1);  
  278. }  
  279.   
  280. function MM_swapImgRestore()  
  281. {  
  282.     var i,x,a=document.MM_sr;  
  283.     for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)  
  284.         x.src=x.oSrc;  
  285. }  
  286.   
  287. function MM_preloadImages() {  
  288.     var d=document;  
  289.     if(d.images)  
  290.     {  
  291.         if(!d.MM_p)  
  292.             d.MM_p = [];  
  293.         var i,j=d.MM_p.length,a=MM_preloadImages.arguments;  
  294.         for(i=0; i<a.length; i++)  
  295.             if (a[i].indexOf("#")!=0)  
  296.             {  
  297.                 d.MM_p[j]=new Image;  
  298.                 d.MM_p[j++].src=a[i];  
  299.             }  
  300.     }  
  301. }  
  302.   
  303. function MM_findObj(n, d)  
  304. {  
  305.     var p,i,x;  
  306.     if(!d)  
  307.         d=document;  
  308.     if((p=n.indexOf("?"))>0&&parent.frames.length)  
  309.     {  
  310.         d=parent.frames[n.substring(p+1)].document;  
  311.         n=n.substring(0,p);  
  312.     }  
  313.     if(!(x=d[n])&&d.all)  
  314.         x=d.all[n];  
  315.     for (i=0;!x&&i<d.forms.length;i++)  
  316.         x=d.forms[i][n];  
  317.     for(i=0;!x&&d.layers&&i<d.layers.length;i++)  
  318.         x=MM_findObj(n,d.layers[i].document);  
  319.     if(!x)  
  320.         x = $(n);  
  321.     return x;  
  322. }  
  323.   
  324. function MM_swapImage()  
  325. {  
  326.     var i,j=0,x,a=MM_swapImage.arguments;  
  327.     document.MM_sr = [];  
  328.     for(i=0;i<(a.length-2);i+=3)  
  329.         if ((x=MM_findObj(a[i]))!=null)  
  330.         {  
  331.             document.MM_sr[j++]=x;  
  332.             if(!x.oSrc)  
  333.                 x.oSrc=x.src;  
  334.             x.src=a[i+2];  
  335.         }  
  336. }  
  337.   
  338. //定位到指定锚位置  
  339. function gotoAnchorLocation(id)  
  340. {  
  341.     var url = self.location.pathname;  
  342.     var search = self.location.search;  
  343.     self.location = url + search + "#cmt" + id;  
  344. }  
  345.   
  346. function trim(str)  
  347. {  
  348.     // from: http://www.cnblogs.com/rubylouvre/archive/2009/09/18/1568794.html  
  349.     if (str.length == 0)  
  350.         return "";  
  351.     str = str.replace(/^/s/s*/, ''),  
  352.     ws = //s/,  
  353.     i = str.length;  
  354.     while (ws.test(str.charAt(--i))) ;  
  355.     return str.slice(0, i + 1);  
  356. }  
  357.   
  358. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/  
  359. function numberFormat (number, decimals, decPoint, thousandsSep) {  
  360.     var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 0 : decimals,  
  361.         d = decPoint === undefined ? "." : decPoint,  
  362.         t = thousandsSep === undefined ? "," : thousandsSep, s = n < 0 ? "-" : "",  
  363.         i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;  
  364.     return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(/d{3})(?=/d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");  
  365. }; 
posted @ 2013-07-03 14:24  小精灵YY  阅读(235)  评论(0)    收藏  举报