常用功能的javascript实现

  1. 文章本自http://shjy-nicholas.javaeye.com/blog/118539
  2. -------------- 函数检索 --------------
  3. */  
  4.   
  5. /**
  6. * 去除多余空格函数
  7. * trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格
  8. * 用法:
  9. *      var str = "   hello ";
  10. *      str = str.trim();
  11. */  
  12. String.prototype.trim = function() {   
  13.     return this.replace(/(^[\\s]*)|([\\s]*$)/g, "");   
  14. }   
  15.   
  16. String.prototype.lTrim = function(){   
  17.     return this.replace(/(^[\\s]*)/g, "");   
  18. }   
  19.   
  20. String.prototype.rTrim = function(){   
  21.     return this.replace(/([\\s]*$)/g, "");   
  22. }   
  23.   
  24. /**
  25. *校验字符串是否为空
  26. *返回值:
  27. *如果不为空,定义校验通过,返回true
  28. *如果为空,校验不通过,返回false
  29. *参考提示信息:输入域不能为空!
  30. */  
  31. function checkIsNotEmpty(str) {   
  32.     if(str.trim() == "")   
  33.         return false;   
  34.     else  
  35.         return true;   
  36. }   
  37.   
  38. /**
  39. *校验字符串是否为整型
  40. *返回值:
  41. *如果为空,定义校验通过,返回true
  42. *如果字串全部为数字,校验通过,返回true
  43. *如果校验不通过,返回false
  44. *参考提示信息:输入域必须为数字!
  45. */  
  46. function checkIsInteger(str){   
  47.     //如果为空,则通过校验   
  48.   if(str == "")   
  49.         return true;   
  50.     if(/^(\\-?)(\\d+)$/.test(str)) {   
  51.         return true;   
  52.      } else {   
  53.         return false;   
  54.      }   
  55. }   
  56.   
  57. *
  58. */  
  59. /**
  60. *校验字符串是否为浮点型
  61. *返回值:
  62. *如果为空,定义校验通过,返回true
  63. *如果字串为浮点型,校验通过,返回true
  64. *如果校验不通过,返回false
  65. *参考提示信息:输入域不是合法的浮点数!
  66. */  
  67. function checkIsDouble(str) {   
  68.     //如果为空,则通过校验   
  69.   if(str == "")   
  70.         return true;   
  71.     //如果是整数,则校验整数的有效性   
  72.   if(str.indexOf(".") == -1) {   
  73.         if(checkIsInteger(str) == true)   
  74.             return true;   
  75.         else  
  76.             return false;   
  77.      } else {   
  78.         if(/^(\\-?)(\\d+)(.{1})(\\d+)$/g.test(str))   
  79.             return true;   
  80.         else  
  81.             return false;   
  82.      }   
  83. }   
  84.   
  85. /**
  86. function isNotNegativeDouble(str) {   
  87.     //如果为空,则通过校验   
  88.   if(str == "")   
  89.         return true;   
  90.     if(checkIsDouble(str) == true) {   
  91.         if(parseFloat(str) < 0)   
  92.             return false;   
  93.         else  
  94.             return true;   
  95.      } else {   
  96.         return false;   
  97.      }   
  98. }   
  99.   
  100. /**
  101. *校验字符串是否为日期型
  102. *返回值:
  103. *如果为空,定义校验通过,返回true
  104. *如果字串为日期型,校验通过,返回true
  105. *如果日期不合法,返回false    
  106. *参考提示信息:输入域的时间不合法!(yyyy-MM-dd)
  107. */  
  108. function checkIsValidDate(str) {   
  109.     //如果为空,则通过校验   
  110.   if(str == "")   
  111.         return true;   
  112.     var pattern = /^((\\d{4})|(\\d{2}))-(\\d{1,2})-(\\d{1,2})$/g;   
  113.     if(!pattern.test(str))   
  114.         return false;   
  115.     var arrDate = str.split("-");   
  116.     if(parseInt(arrDate[0],10) < 100)   
  117.          arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";   
  118.     var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);   
  119.     if(date.getYear() == arrDate[0]   
  120.         && date.getMonth() == (parseInt(arrDate[1],10) -1)+""  
  121.         && date.getDate() == arrDate[2])   
  122.         return true;   
  123.     else  
  124.         return false;   
  125. }   
  126.   
  127. /**
  128. *校验两个日期的先后
  129. *返回值:
  130. *如果其中有一个日期为空,校验通过。 返回true
  131. *如果起始日期早于等于终止日期,校验通过,返回true
  132. *如果起始日期晚于终止日期,返回false    
  133. *参考提示信息: 起始日期不能晚于结束日期。
  134. */  
  135. function checkDateEarlier(strStart,strEnd) {   
  136.     if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)   
  137.         return false;   
  138.     //如果有一个输入为空,则通过检验   
  139.   if (( strStart == "" ) || ( strEnd == "" ))   
  140.         return true;   
  141.     var arr1 = strStart.split("-");   
  142.     var arr2 = strEnd.split("-");   
  143.     var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);   
  144.     var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);   
  145.     if(arr1[1].length == 1)   
  146.          arr1[1] = "0" + arr1[1];   
  147.     if(arr1[2].length == 1)   
  148.          arr1[2] = "0" + arr1[2];   
  149.     if(arr2[1].length == 1)   
  150.          arr2[1] = "0" + arr2[1];   
  151.     if(arr2[2].length == 1)   
  152.          arr2[2]="0" + arr2[2];   
  153.     var d1 = arr1[0] + arr1[1] + arr1[2];   
  154.     var d2 = arr2[0] + arr2[1] + arr2[2];   
  155.     if(parseInt(d1,10) > parseInt(d2,10))   
  156.        return false;   
  157.     else  
  158.        return true;   
  159. }   
  160.   
  161. /**
  162. *校验字符串是否为email型
  163. *返回值:
  164. *如果为空,定义校验通过,返回true
  165. *如果字串为email型,校验通过,返回true
  166. *如果email不合法,返回false    
  167. *参考提示信息:Email的格式不正確!
  168. */  
  169. function checkEmail(str) {   
  170.     //如果为空,则通过校验   
  171.   if(str == "")   
  172.         return true;   
  173.     if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf(\'@\', 0) == -1   
  174.          || str.indexOf(\'.\', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)   
  175.         return false;   
  176.     else  
  177.         return true;   
  178. }   
  179.   
  180. /**
  181. *校验字符串是否为中文
  182. *返回值:
  183. *如果为空,定义校验通过,返回true
  184. *如果字串为中文,校验通过,返回true
  185. *如果字串为非中文,返回false    
  186. *参考提示信息:必须为中文!
  187. */  
  188. function checkIsChinese(str) {   
  189.     //如果值为空,通过校验   
  190.   if (str == "")   
  191.         return true;   
  192.     var pattern = /^([\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0])*$/gi;   
  193.     if (pattern.test(str))   
  194.         return true;   
  195.     else  
  196.         return false;   
  197. }   
  198.   
  199. /**
  200. * 计算字符串的长度,一个汉字两个字符
  201. */  
  202. String.prototype.realLength = function() {   
  203.   return this.replace(/[^\\x00-\\xff]/g,"**").length;   
  204. }   
  205.   
  206. /**
  207. *校验字符串是否符合自定义正则表达式
  208. *str 要校验的字串   pat 自定义的正则表达式
  209. *返回值:
  210. *如果为空,定义校验通过,返回true
  211. *如果字串符合,校验通过,返回true
  212. *如果字串不符合,返回false
  213. *参考提示信息:必须满足***模式
  214. */  
  215. function checkMask(str,pat) {   
  216.     //如果值为空,通过校验   
  217.   if (str == "")   
  218.         return true;   
  219.     var pattern = new RegExp(pat,"gi")   
  220.     if (pattern.test(str))   
  221.         return true;   
  222.     else  
  223.         return false;   
  224. }   
  225.   
  226. /**
  227. * 得到文件的后缀名
  228. * oFile为file控件对象
  229. */  
  230. function getFilePostfix(oFile) {   
  231.     if(oFile == null)   
  232.         return null;   
  233.     var pattern = /(.*)\\.(.*)$/gi;   
  234.     if(typeof(oFile) == "object") {   
  235.         if(oFile.value == null || oFile.value == "")   
  236.             return null;   
  237.         var arr = pattern.exec(oFile.value);   
  238.         return RegExp.$2;   
  239.      } else if(typeof(oFile) == "string") {   
  240.         var arr = pattern.exec(oFile);   
  241.         return RegExp.$2;   
  242.      } else {   
  243.         return null;   
  244.      }   

你可能感兴趣的:(JavaScript)