js常用工具类(二次更新)

本文介绍JavaScript中常用的字符串处理方法,如去除空格、显示错误信息、检查空值和长度、下拉列表选择、判断数值和中文、复选框全选与取消功能,并详细阐述了一个日期处理类的实现,包括获取当前日期、年份、月份、日、小时、分钟、秒及日期范围等。此外,还提供了验证字符串位数、邮箱格式、空串、等效字符串、有效位数范围的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Js代码  收藏代码
  1. js常用方法将会不断更新  
Js代码  收藏代码
  1. /* 
  2. 说明:去除字符串两边空格函数 
  3. 参数obj:要去除空格的文本框 
  4. 返回值:去除空格之后的字符串 
  5. */  
  6. function trim(obj) {  
  7.     return String(obj.value).replace(/(^\s*)|(\s*$)/g, "");  
  8. }  
  9.   
  10. /* 
  11. 说明:显示错误信息函数 
  12. 参数obj:出现错误信息的文本框 
  13. 参数errmsg:错误信息 
  14. */  
  15. function showError(obj, errmsg) {  
  16.     alert(errmsg);  
  17.     try{  
  18.         obj.focus();  
  19.     } catch(e) {  
  20.     }  
  21. }  
  22.   
  23. /* 
  24. 说明:检查是否为空函数 
  25. 参数obj:要检查的文本框 
  26. 返回值:判断结果 true不为空 false为空 
  27. */  
  28. function checkEmpty(obj) {  
  29.     if(obj == "") {  
  30.         return false;  
  31.     } else {  
  32.         return true;  
  33.     }  
  34. }  
  35.   
  36. /* 
  37. 说明:检查长度函数 
  38. 参数obj:要检查长度的文本框 
  39. 参数min:最小长度 
  40. 参数max:最大长度 
  41. 返回值:判断结果 true在要求长度中 false超出要求长度 
  42. */  
  43. function checkLength(obj, min, max) {  
  44.     if(obj.length < min || obj.length > max) {  
  45.         return false;  
  46.     } else {  
  47.         return true;  
  48.     }  
  49. }  
  50.   
  51. /* 
  52. 说明:下拉列表选中函数 
  53. 参数obj:要选中的下拉列表 
  54. 参数selectvalue:标识选中的参数 
  55. */  
  56. function selectitem(obj , selectvalue){  
  57.     var options = obj.options;  
  58.     for(var i = 0; i < options.length; i++) {  
  59.         if(selectvalue == options[i].value) {  
  60.             options[i].selected = true;  
  61.         }  
  62.     }     
  63. }  
  64.   
  65. /* 
  66. 说明:判断value变量值是否是数字 
  67. 参数value:输入值 
  68. 返回值:是数字返回true,否则false 
  69. */  
  70. function isNumeric(value){  
  71.     if( value != null && value.length>0 && isNaN(value) == false){  
  72.         return true;  
  73.     }  
  74.     else{  
  75.         return false;  
  76.     }  
  77. }     
  78.   
  79. /* 
  80. 说明:判断value变量值是否是中文 
  81. 参数value:输入值 
  82. 返回值:是中文返回false,否则true 
  83. */  
  84. function isChn(str){  
  85.         var reg = /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/;  
  86.         if(reg.test(str)){  
  87.             return false;  
  88.         }  
  89.         return true;  
  90. }  
  91.   
  92.   
  93. /* 
  94. 说明:对复选框的全选或不选 
  95. 参数state:输入值 1 全选 2 全部选 
  96. 返回值:是中文返回false,否则true 
  97. */  
  98. function change(state){  
  99.     try{  
  100.         var checks=document.getElementsByTagName("input");  
  101.         var i=0;  
  102.         var length=checks.length;  
  103.         var flag=true;  
  104.         if(state==1){  
  105.             flag=true;  
  106.         }  
  107.         if(state==0){  
  108.             flag=false;  
  109.         }  
  110.         for(i;i<length;i++){  
  111.             if(checks[i].type=="checkbox"){  
  112.                 checks[i].checked=flag;  
  113.             }  
  114.         }  
  115.     }catch(e){  
  116.         window.alert(e.message);  
  117.     }  
  118. }  

 

Js代码  收藏代码
  1. /** 
  2.  * <code>DateUtil</code>类用于封装常用的日期处理操作 
  3.  */  
  4. var DateUtil = function(year,month,day,hour,minute,second){  
  5.     /** curDateTime     当前客户端日期时间*/  
  6.     this.curDateTime = new Date();    
  7.     /** 
  8.      * <code>getDateTime</code>方法返回Date类型对象 
  9.      *  
  10.      */  
  11.     this.getDateTime = function(){  
  12.         var date = null;  
  13.         if((year==null && month==null && day==null  
  14.                 && hour == null && minute == null && second == null)){  
  15.             date =  this.curDateTime;  
  16.         }else if(year != null && month != null && day != null  
  17.                     && hour == null && minute == null && second == null){  
  18.             date = new Date(year,month-1,day);  
  19.         }else if(year != null && month != null && day != null  
  20.                     && hour != null && minute != null && second != null){  
  21.             date = new Date(year,month-1,day,hour,minute,second);             
  22.         }  
  23.         return date;              
  24.     };    
  25.       
  26.     /** 
  27.      * <code>getYear</code>方法取得年值 
  28.      *  
  29.      */  
  30.     this.getYear = function(){  
  31.         var year = null;  
  32.         var dateTime = this.getDateTime();                        
  33.   
  34.               
  35.         if(dateTime != null){  
  36.             year = dateTime.getFullYear();  
  37.         }else{  
  38.             year = this.curDateTime.getFullYear();  
  39.         }         
  40.         return year;  
  41.     };  
  42.       
  43.     /** 
  44.      * <code>getMonth</code>方法取得月值 
  45.      *  
  46.      */  
  47.     this.getMonth = function(){  
  48.         var month = null;  
  49.         var dateTime = this.getDateTime();  
  50.         if(dateTime != null){  
  51.             month = dateTime.getMonth() + 1;          
  52.         }else{  
  53.             month = this.curDateTime.getMonth() + 1;      
  54.         }  
  55.         return month;  
  56.     };  
  57.       
  58.     /** 
  59.      * <code>getDay</code>方法取得日值 
  60.      *  
  61.      */  
  62.     this.getDay = function(){  
  63.         var day = null;  
  64.         var dateTime = this.getDateTime();  
  65.         if(dateTime != null){  
  66.             day = dateTime.getDate();  
  67.         }else{  
  68.             day = this.curDateTime.getDate();     
  69.         }  
  70.         return day;  
  71.     };  
  72.       
  73.     /** 
  74.      * <code>getHour</code>方法取得24进制小时 
  75.      *  
  76.      */  
  77.     this.getHour = function(){  
  78.         var hour = null;  
  79.         var dateTime = this.getDateTime();  
  80.         if(dateTime != null){  
  81.             hour = dateTime.getHours();  
  82.         }else{  
  83.             hour = this.curDateTime.getHours();   
  84.         }  
  85.         return hour;  
  86.     };    
  87.       
  88.     /** 
  89.      * <code>getMinute</code>方法取得分值 
  90.      *  
  91.      */  
  92.     this.getMinute = function(){  
  93.         var minute = null;  
  94.         var dateTime = this.getDateTime();  
  95.         if(dateTime != null){  
  96.             minute = dateTime.getMinutes();  
  97.         }else{  
  98.             minute = this.curDateTime.getMinutes();  
  99.         }  
  100.         return minute;        
  101.     };  
  102.       
  103.         /** 
  104.      * <code>getSecond</code>方法取得秒值 
  105.      *  
  106.      */  
  107.     this.getSecond = function(){  
  108.         var second = null;  
  109.         var dateTime = this.getDateTime();  
  110.         if(dateTime != null){  
  111.             second = dateTime.getSeconds();  
  112.         }else{  
  113.             second = this.curDateTime.getSeconds();  
  114.         }  
  115.         return second;  
  116.     };  
  117.       
  118.       
  119.     /** 
  120.      * <code>getDateRange</code>方法用于得到一天之内的时刻范围 
  121.      *  
  122.      * @return range ["凌晨"|"上午"|"中午"|"下午"|"晚上"] 
  123.      */  
  124.     this.getDateRange = function(){  
  125.         var hour = window.parseInt(this.getHour());  
  126.         var range = "凌晨"  
  127.         if(hour >= 6 && hour < 11){  
  128.             range = "早晨";  
  129.         }else if(hour >=11 && hour < 14){  
  130.             range = "中午";  
  131.         }else if(hour >=14 && hour <= 18){  
  132.             range = "下午";  
  133.         }else if(hour >18 && hour < 24){  
  134.             range = "晚上";  
  135.         }  
  136.         return range;  
  137.     };    
  138.     /** 
  139.      * <code>get12PatternHour</code>方法用于得到12进制小时值 
  140.      *  
  141.      */  
  142.     this.get12PatternHour = function(){  
  143.         return hour>12?(hour+12-24):hour;  
  144.     };  
  145.     /** 
  146.      * <code>isLeapYear</code>方法用于判断是否为闰年 
  147.      * <p> 
  148.      * 闰年算法说明: 
  149.      * 能被4整除并且不能被100整除或者能被400整除的年份是闰年 
  150.      */  
  151.     this.isLeapYear = function(){  
  152.         var flag = false;  
  153.         if((this.getYear() % 4 == 0 && this.getYear() % 100 !=0)  
  154.                 || (this.getYear() % 400 == 0)){  
  155.             flag = true;  
  156.         }         
  157.         return flag;  
  158.     };  
  159.       
  160.   
  161.     /** 
  162.      * <code>getMaxDaysByMonth</code>方法根据月份获取该月的最大天数 
  163.      *   
  164.      */  
  165.     this.getMaxDaysByMonth = function(){  
  166.         var days = 31;  
  167.         var month = this.getMonth();  
  168.         switch(month){  
  169.             case 2:  
  170.                 if(this.isLeapYear()){  
  171.                     days = 29;  
  172.                 }else{  
  173.                     days = 28;  
  174.                 }  
  175.                 break;  
  176.             case 4:  
  177.             case 6:  
  178.             case 9:  
  179.             case 11:  
  180.                 days = 30;  
  181.                 break;  
  182.             default:  
  183.                 break;  
  184.         }  
  185.         return days;  
  186.     }  
  187. }  
  188.   
  189.   
  190. /** 
  191.  * <code>isEmptyString</code>方法用于检查字符串是否为空字符串 
  192.  * <p> 
  193.  * @return boolean false → 不是空串 true → 是空串  
  194.  */  
  195.   
  196. function isEmptyStr(str){  
  197.     if(trim(str).length == 0 || str==null){  
  198.         return true;  
  199.     }else{                
  200.         return false;  
  201.     }  
  202. }  
  203.   
  204.   
  205. /** 
  206.  * <code>isEqualString</code>方法用于检查两个字符串是否相等 
  207.  * <p> 
  208.  * @return  boolean false → 不相等 true → 相等 
  209.  */  
  210. function isEqualStr(str1,str2){  
  211.     if(str1 == str2){  
  212.         return true;  
  213.     }else{  
  214.         return false;  
  215.     }  
  216. }  
  217.   
  218. /** 
  219.  * <code>isValidateCols</code>方法用于检查字符串是否是有效位数 
  220.  * <p> 
  221.  * @return boolean false → 不是制定位数 true → 是指定位数 
  222.  */  
  223. function isValidateMinCols(str,cols){  
  224.     if(str.length >= cols){  
  225.         return true;  
  226.     }else{  
  227.         return false;  
  228.     }  
  229. }  
  230.   
  231. function isValidateMaxCols(str,cols){  
  232.     if(str.length <= cols){  
  233.         return true;  
  234.     }else{  
  235.         return false;  
  236.     }  
  237. }  
  238.   
  239. function isValidateRangeCols(str,min,max){  
  240.     if(str.length >= min   
  241.         && str.length <=max){  
  242.         return true;  
  243.     }else{  
  244.         return false;  
  245.     }  
  246. }  
  247.   
  248. /** 
  249.  * <code>isValidateEmail<code>方法用于检查email格式是否正确 
  250.  * <p> 
  251.  * @return boolean false → 无效Email true → 有效Email 
  252.  */  
  253. function isValidateEmail(email){  
  254.      var emailPattern = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))" +  
  255.                 //"@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil" +  
  256.                 "@([a-zA-Z0-9-]+[.])+(cn|net|NET|com|COM|gov|GOV|mil" +  
  257.                 "|MIL|org|ORG|edu|EDU|int|INT)$"  
  258.      var re = new RegExp(emailPattern);  
  259.      if(re.test(email)){  
  260.         return true;  
  261.      }else{  
  262.         return false;  
  263.      }  
  264. }  
  265. /** 
  266.  * <code>trim</code>方法用于去掉字符串两边的空格 
  267.  * <p> 
  268.  */  
  269. function trim(str){   
  270.     str = trimLeft(trimRight(str));  
  271.     return str;   
  272. }  
  273.   
  274. /** 
  275.  * <code>trimLeft</code>方法用于去除字符串左侧的空格 
  276.  * <p> 
  277.  * @param   str 预处理的字符串 
  278.  * @return  去掉左侧空格的字符串 
  279.  */  
  280. function trimLeft(str){   
  281.     var pattern = /^\s/;  
  282.     while(pattern.test(str)){         
  283.         str = str.substring(1);  
  284.     }     
  285.     return str;  
  286. }  
  287.   
  288. /** 
  289.  * <code>trimRight</code>方法用于去除字符串右侧的空格 
  290.  * <p> 
  291.  * @param   str 预处理字符串 
  292.  * @return  去掉右侧空格的字符串   
  293.  */  
  294. function trimRight(str){  
  295.     var pattern = /\s$/;  
  296.     while(pattern.test(str)){  
  297.         str = str.substring(0,str.length-1);  
  298.     }  
  299.     return str;  
  300. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值