常用功能的Javascript实现

常用功能的javascript实现

-------摘抄自《tech.163》,并经过修改,以便今后查看之用           

js 代码
  1. /*  
  2. -------------- 函数检索 --------------  
  3. trim函数:                         trim() lTrim() rTrim()  
  4. 校验字符串是否为空:                 checkIsNotEmpty(str)  
  5. 校验字符串是否为整型:               checkIsInteger(str)  
  6. 校验整型最小值:                    checkIntegerMinValue(str,val)  
  7. 校验整型最大值:                    checkIntegerMaxValue(str,val)  
  8. 校验整型是否为非负数:               isNotNegativeInteger(str)  
  9. 校验字符串是否为浮点型:             checkIsDouble(str)  
  10. 校验浮点型最小值:                  checkDoubleMinValue(str,val)  
  11. 校验浮点型最大值:                  checkDoubleMaxValue(str,val)  
  12. 校验浮点型是否为非负数:             isNotNegativeDouble(str)  
  13. 校验字符串是否为日期型:             checkIsValidDate(str)  
  14. 校验两个日期的先后:                checkDateEarlier(strStart,strEnd)  
  15. 校验字符串是否为email型:           checkEmail(str)  
  16.  
  17. 校验字符串是否为中文:               checkIsChinese(str)  
  18. 计算字符串的长度,一个汉字两个字符:   realLength()  
  19. 校验字符串是否符合自定义正则表达式:   checkMask(str,pat)  
  20. 得到文件的后缀名:                   getFilePostfix(oFile)   
  21. -------------- 函数检索 --------------  
  22. */  
  23.   
  24. /**  
  25. * 去除多余空格函数  
  26. * trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格  
  27. * 用法:  
  28. *     var str = "  hello ";  
  29. *     str = str.trim();  
  30. */  
  31. String.prototype.trim = function() {   
  32.     return this.replace(/(^[\\s]*)|([\\s]*$)/g, "");   
  33. }   
  34.   
  35. String.prototype.lTrim = function(){   
  36.     return this.replace(/(^[\\s]*)/g, "");   
  37. }   
  38.   
  39. String.prototype.rTrim = function(){   
  40.     return this.replace(/([\\s]*$)/g, "");   
  41. }    
  42.   
  43. /**  
  44. *校验字符串是否为空  
  45. *返回值:  
  46. *如果不为空,定义校验通过,返回true  
  47. *如果为空,校验不通过,返回false   
  48. *参考提示信息:输入域不能为空!  
  49. */  
  50. function checkIsNotEmpty(str) {   
  51.     if(str.trim() == "")   
  52.         return false;   
  53.     else  
  54.         return true;   
  55. }   
  56.   
  57. /**  
  58. *校验字符串是否为整型  
  59. *返回值:  
  60. *如果为空,定义校验通过,返回true  
  61. *如果字串全部为数字,校验通过,返回true  
  62. *如果校验不通过,返回false   
  63. *参考提示信息:输入域必须为数字!  
  64. */  
  65. function checkIsInteger(str){   
  66.     //如果为空,则通过校验   
  67.   if(str == "")   
  68.         return true;   
  69.     if(/^(\\-?)(\\d+)$/.test(str)) {   
  70.         return true;   
  71.     } else {   
  72.         return false;   
  73.     }   
  74. }   
  75.   
  76. /**  
  77. *校验整型最小值  
  78. *str:要校验的串。val:比较的值  
  79. *  
  80. *返回值:  
  81. *如果为空,定义校验通过,返回true  
  82. *如果满足条件,大于等于给定值,校验通过,返回true  
  83. *如果小于给定值,返回false  
  84. *参考提示信息:输入域不能小于给定值!  
  85. */  
  86. function checkIntegerMinValue(str,val) {   
  87.     //如果为空,则通过校验   
  88.     if(str == "")   
  89.             return true;   
  90.         if(typeof(val) != "string")   
  91.             val = val + "";   
  92.         if(checkIsInteger(str) == true){   
  93.             if(parseInt(str,10)>=parseInt(val,10))   
  94.                 return true;   
  95.             else  
  96.                 return false;   
  97.         } else {   
  98.             return false;   
  99.         }   
  100. }   
  101.   
  102. /**  
  103. *校验整型最大值  
  104. *str:要校验的串。val:比较的值  
  105. *  
  106. *返回值:  
  107. *如果为空,定义校验通过,返回true  
  108. *如果满足条件,小于等于给定值,校验通过,返回true  
  109. *如果大于给定值,返回false         
  110. *参考提示信息:输入值不能大于给定值!  
  111. */  
  112. function checkIntegerMaxValue(str,val) {   
  113.     //如果为空,则通过校验   
  114.   if(str == "")   
  115.         return true;   
  116.     if(typeof(val) != "string")   
  117.         val = val + "";   
  118.     if(checkIsInteger(str) == true){   
  119.         if(parseInt(str,10)<=parseInt(val,10))   
  120.             return true;   
  121.         else  
  122.             return false;   
  123.     } else {   
  124.         return false;   
  125.     }   
  126. }   
  127.   
  128. /**  
  129. *校验整型是否为非负数  
  130. *str:要校验的串。  
  131. *  
  132. *返回值:  
  133. *如果为空,定义校验通过,返回true  
  134. *如果非负数,返回true  
  135. *如果是负数,返回false   
  136. *参考提示信息:输入值不能是负数!  
  137. */  
  138. function isNotNegativeInteger(str){   
  139.     //如果为空,则通过校验   
  140.   if(str == "")   
  141.         return true;   
  142.     if(checkIsInteger(str) == true){   
  143.         if(parseInt(str,10) < 0)   
  144.             return false;   
  145.         else  
  146.             return true;   
  147.     }else{   
  148.         return false;   
  149.     }   
  150. }   
  151.   
  152. /**  
  153. *校验字符串是否为浮点型  
  154. *返回值:  
  155. *如果为空,定义校验通过,返回true  
  156. *如果字串为浮点型,校验通过,返回true  
  157. *如果校验不通过,返回false  
  158. *参考提示信息:输入域不是合法的浮点数!  
  159. */  
  160. function checkIsDouble(str) {   
  161.     //如果为空,则通过校验   
  162.   if(str == "")   
  163.         return true;   
  164.     //如果是整数,则校验整数的有效性   
  165.   if(str.indexOf(".") == -1) {   
  166.         if(checkIsInteger(str) == true)   
  167.             return true;   
  168.         else  
  169.             return false;   
  170.     } else {   
  171.         if(/^(\\-?)(\\d+)(.{1})(\\d+)$/g.test(str))   
  172.             return true;   
  173.         else  
  174.             return false;   
  175.     }   
  176. }   
  177.   
  178. /**  
  179. *校验浮点型最小值  
  180. *str:要校验的串。val:比较的值  
  181. *  
  182. *返回值:  
  183. *如果为空,定义校验通过,返回true  
  184. *如果满足条件,大于等于给定值,校验通过,返回true  
  185. *如果小于给定值,返回false               
  186. * 参考提示信息:输入域不能小于给定值!  
  187. */  
  188. function checkDoubleMinValue(str,val) {   
  189.     //如果为空,则通过校验   
  190.   if(str == "")   
  191.         return true;   
  192.     if(typeof(val) != "string")   
  193.         val = val + "";   
  194.     if(checkIsDouble(str) == true){   
  195.         if(parseFloat(str)>=parseFloat(val))   
  196.             return true;   
  197.         else  
  198.             return false;   
  199.     } else {   
  200.         return false;   
  201.     }   
  202. }   
  203.   
  204. /**  
  205. *校验浮点型最大值  
  206. *str:要校验的串。  val:比较的值  
  207. *  
  208. *返回值:  
  209. *如果为空,定义校验通过,返回true  
  210. *如果满足条件,小于等于给定值,校验通过,返回true  
  211. *如果大于给定值,返回false         
  212. *参考提示信息:输入值不能大于给定值!  
  213. */  
  214. function checkDoubleMaxValue(str,val) {   
  215.     //如果为空,则通过校验   
  216.   if(str == "")   
  217.         return true;   
  218.     if(typeof(val) != "string")   
  219.         val = val + "";   
  220.     if(checkIsDouble(str) == true) {   
  221.         if(parseFloat(str)<=parseFloat(val))   
  222.             return true;   
  223.         else  
  224.             return false;   
  225.     } else {   
  226.         return false;   
  227.     }   
  228. }   
  229.   
  230. /**  
  231. *校验浮点型是否为非负数  
  232. *str:要校验的串。  
  233. *  
  234. *返回值:  
  235. *如果为空,定义校验通过,返回true  
  236. *如果非负数,返回true  
  237. *如果是负数,返回false  
  238. *参考提示信息:输入值不能是负数!  
  239. */  
  240. function isNotNegativeDouble(str) {   
  241.     //如果为空,则通过校验   
  242.   if(str == "")   
  243.         return true;   
  244.     if(checkIsDouble(str) == true) {   
  245.         if(parseFloat(str) < 0)   
  246.             return false;   
  247.         else  
  248.             return true;   
  249.     } else {   
  250.         return false;   
  251.     }   
  252. }   
  253.   
  254. /**  
  255. *校验字符串是否为日期型  
  256. *返回值:  
  257. *如果为空,定义校验通过,返回true  
  258. *如果字串为日期型,校验通过,返回true  
  259. *如果日期不合法,返回false      
  260. *参考提示信息:输入域的时间不合法!(yyyy-MM-dd)  
  261. */  
  262. function checkIsValidDate(str) {   
  263.     //如果为空,则通过校验   
  264.   if(str == "")   
  265.         return true;   
  266.     var pattern = /^((\\d{4})|(\\d{2}))-(\\d{1,2})-(\\d{1,2})$/g;   
  267.     if(!pattern.test(str))   
  268.         return false;   
  269.     var arrDate = str.split("-");   
  270.     if(parseInt(arrDate[0],10) < 100)   
  271.         arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";   
  272.     var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);   
  273.     if(date.getYear() == arrDate[0]   
  274.        && date.getMonth() == (parseInt(arrDate[1],10) -1)+""  
  275.        && date.getDate() == arrDate[2])   
  276.         return true;   
  277.     else  
  278.         return false;   
  279. }   
  280.   
  281. /**  
  282. *校验两个日期的先后  
  283. *返回值:  
  284. *如果其中有一个日期为空,校验通过。 返回true  
  285. *如果起始日期早于等于终止日期,校验通过,返回true  
  286. *如果起始日期晚于终止日期,返回false      
  287. *参考提示信息: 起始日期不能晚于结束日期。  
  288. */  
  289. function checkDateEarlier(strStart,strEnd) {   
  290.     if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)   
  291.         return false;   
  292.     //如果有一个输入为空,则通过检验   
  293.   if (( strStart == "" ) || ( strEnd == "" ))   
  294.         return true;   
  295.     var arr1 = strStart.split("-");   
  296.     var arr2 = strEnd.split("-");   
  297.     var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);   
  298.     var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);   
  299.     if(arr1[1].length == 1)   
  300.         arr1[1] = "0" + arr1[1];   
  301.     if(arr1[2].length == 1)   
  302.         arr1[2] = "0" + arr1[2];   
  303.     if(arr2[1].length == 1)   
  304.         arr2[1] = "0" + arr2[1];   
  305.     if(arr2[2].length == 1)   
  306.         arr2[2]="0" + arr2[2];   
  307.     var d1 = arr1[0] + arr1[1] + arr1[2];   
  308.     var d2 = arr2[0] + arr2[1] + arr2[2];   
  309.     if(parseInt(d1,10) > parseInt(d2,10))   
  310.        return false;   
  311.     else  
  312.        return true;   
  313. }   
  314.   
  315. /**  
  316. *校验字符串是否为email型  
  317. *返回值:  
  318. *如果为空,定义校验通过,返回true  
  319. *如果字串为email型,校验通过,返回true  
  320. *如果email不合法,返回false      
  321. *参考提示信息:Email的格式不正確!  
  322. */  
  323. function checkEmail(str) {   
  324.     //如果为空,则通过校验   
  325.   if(str == "")   
  326.         return true;   
  327.     if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf(\'@\', 0) == -1   
  328.         || str.indexOf(\'.\', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)   
  329.         return false;   
  330.     else  
  331.         return true;   
  332. }   
  333.   
  334. /**  
  335. *校验字符串是否为中文  
  336. *返回值:  
  337. *如果为空,定义校验通过,返回true  
  338. *如果字串为中文,校验通过,返回true  
  339. *如果字串为非中文,返回false      
  340. *参考提示信息:必须为中文!  
  341. */  
  342. function checkIsChinese(str) {   
  343.     //如果值为空,通过校验   
  344.   if (str == "")   
  345.         return true;   
  346.     var pattern = /^([\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0])*$/gi;   
  347.     if (pattern.test(str))   
  348.         return true;   
  349.     else  
  350.         return false;   
  351. }   
  352.   
  353. /**  
  354. * 计算字符串的长度,一个汉字两个字符  
  355. */  
  356. String.prototype.realLength = function() {   
  357.   return this.replace(/[^\\x00-\\xff]/g,"**").length;   
  358. }   
  359.   
  360. /**  
  361. *校验字符串是否符合自定义正则表达式  
  362. *str 要校验的字串  pat 自定义的正则表达式  
  363. *返回值:  
  364. *如果为空,定义校验通过,返回true  
  365. *如果字串符合,校验通过,返回true  
  366. *如果字串不符合,返回false  
  367. *参考提示信息:必须满足***模式  
  368. */  
  369. function checkMask(str,pat) {   
  370.     //如果值为空,通过校验   
  371.   if (str == "")   
  372.         return true;   
  373.     var pattern = new RegExp(pat,"gi")   
  374.     if (pattern.test(str))   
  375.         return true;   
  376.     else  
  377.         return false;   
  378. }   
  379.   
  380. /**  
  381. * 得到文件的后缀名  
  382. * oFile为file控件对象  
  383. */  
  384. function getFilePostfix(oFile) {   
  385.     if(oFile == null)   
  386.         return null;   
  387.     var pattern = /(.*)\\.(.*)$/gi;   
  388.     if(typeof(oFile) == "object") {   
  389.         if(oFile.value == null || oFile.value == "")   
  390.             return null;   
  391.         var arr = pattern.exec(oFile.value);   
  392.         return RegExp.$2;   
  393.     } else if(typeof(oFile) == "string") {   
  394.         var arr = pattern.exec(oFile);   
  395.         return RegExp.$2;   
  396.     } else {   
  397.         return null;   
  398.     }   
  399. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值