javascript函数库(全)

来源:互联网 发布:创业如何组建团队 知乎 编辑:程序博客网 时间:2024/06/02 01:53

/*

————– 函数检索 ————–
trim函数: trim() lTrim() rTrim()
校验字符串是否为空: checkIsNotEmpty(str)
校验字符串是否为整型: checkIsInteger(str)
校验整型最小值: checkIntegerMinValue(str,val)
校验整型最大值: checkIntegerMaxValue(str,val)
校验整型是否为非负数: isNotNegativeInteger(str)
校验字符串是否为浮点型: checkIsDouble(str)
校验浮点型最小值: checkDoubleMinValue(str,val)
校验浮点型最大值: checkDoubleMaxValue(str,val)
校验浮点型是否为非负数: isNotNegativeDouble(str)
校验字符串是否为日期型: checkIsValidDate(str)
校验两个日期的先后: checkDateEarlier(strStart,strEnd)
校验字符串是否为email型: checkEmail(str)

校验字符串是否为中文: checkIsChinese(str)
计算字符串的长度,一个汉字两个字符: realLength()
校验字符串是否符合自定义正则表达式: checkMask(str,pat)
得到文件的后缀名: getFilePostfix(oFile)
————– 函数检索 ————–
*/

  1. /**
  2. * added by LxcJie 2004.6.25
  3. * 去除多余空格函数
  4. * trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格
  5. * 用法:
  6. *     var str = "  hello ";
  7. *     str = str.trim();
  8. */
  9. String.prototype.trim = function()
  10. {
  11.     return this.replace(/(^[/s]*)|([/s]* $)/g, "");
  12. }
  13. String.prototype.lTrim = function()
  14. {
  15.     return this.replace(/(^[/s]*)/g, "");
  16. }
  17. String.prototype.rTrim = function()
  18. {
  19.     return this.replace(/([/s]* $)/g, "");
  20. }

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