1. -------------- 函数检索 --------------   
  2.  trim函数:                         trim() lTrim() rTrim()   
  3.  校验字符串是否为空:                 checkIsNotEmpty(str)   
  4.  校验字符串是否为整型:               checkIsInteger(str)   
  5.  校验整型最小值:                    checkIntegerMinValue(str,val)   
  6.  校验整型最大值:                    checkIntegerMaxValue(str,val)    
  7.  校验整型是否为非负数:               isNotNegativeInteger(str)   
  8.  校验字符串是否为浮点型:             checkIsDouble(str)    
  9.  校验浮点型最小值:                  checkDoubleMinValue(str,val)   
  10.  校验浮点型最大值:                  checkDoubleMaxValue(str,val)   
  11.  校验浮点型是否为非负数:             isNotNegativeDouble(str)   
  12.  校验字符串是否为日期型:             checkIsValidDate(str)   
  13.  校验两个日期的先后:                checkDateEarlier(strStart,strEnd)   
  14.  校验字符串是否为email型:           checkEmail(str)   
  15.     
  16.  校验字符串是否为中文:               checkIsChinese(str)   
  17.  计算字符串的长度,一个汉字两个字符:   realLength()   
  18.  校验字符串是否符合自定义正则表达式:   checkMask(str,pat)   
  19.  得到文件的后缀名:                   getFilePostfix(oFile)     
  20.  -------------- 函数检索 --------------   
  21. */    
  22.     
  23.   
  24. /**  
  25.  * added by LxcJie 2004.6.25  
  26.  * 去除多余空格函数  
  27.  * trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格  
  28.  * 用法:  
  29.  *     var str = "  hello ";  
  30.  *     str = str.trim();  
  31.  */  
  32. String.prototype.trim = function()   
  33. {   
  34.     return this.replace(/(^["s]*)|(["s]*$)/g, "");   
  35. }   
  36. String.prototype.lTrim = function()   
  37. {   
  38.     return this.replace(/(^["s]*)/g, "");   
  39. }   
  40. String.prototype.rTrim = function()   
  41. {   
  42.     return this.replace(/(["s]*$)/g, "");   
  43. }   
  44. /********************************** Empty **************************************/  
  45. /**  
  46. *校验字符串是否为空  
  47. *返回值:  
  48. *如果不为空,定义校验通过,返回true  
  49. *如果为空,校验不通过,返回false               参考提示信息:输入域不能为空!  
  50. */  
  51. function checkIsNotEmpty(str)   
  52. {   
  53.     if(str.trim() == "")   
  54.         return false;   
  55.     else  
  56.         return true;   
  57. }//~~~   
  58. /*--------------------------------- Empty --------------------------------------*/  
  59. /********************************** Integer *************************************/  
  60. /**  
  61. *校验字符串是否为整型  
  62. *返回值:  
  63. *如果为空,定义校验通过,      返回true  
  64. *如果字串全部为数字,校验通过,返回true  
  65. *如果校验不通过,              返回false     参考提示信息:输入域必须为数字!  
  66. */  
  67. function checkIsInteger(str)   
  68. {   
  69.     //如果为空,则通过校验   
  70.     if(str == "")   
  71.         return true;   
  72.     if(/^("-?)("d+)$/.test(str))   
  73.         return true;   
  74.     else  
  75.         return false;   
  76. }//~~~   
  77. /**  
  78. *校验整型最小值  
  79. *str:要校验的串。  val:比较的值  
  80. *  
  81. *返回值:  
  82. *如果为空,定义校验通过,                返回true  
  83. *如果满足条件,大于等于给定值,校验通过,返回true  
  84. *如果小于给定值,                        返回false              参考提示信息:输入域不能小于给定值!  
  85. */  
  86. function checkIntegerMinValue(str,val)   
  87. {   
  88.     //如果为空,则通过校验   
  89.     if(str == "")   
  90.         return true;   
  91.     if(typeof(val) != "string")   
  92.         val = val + "";   
  93.     if(checkIsInteger(str) == true)   
  94.     {   
  95.         if(parseInt(str,10)>=parseInt(val,10))   
  96.             return true;   
  97.         else  
  98.             return false;   
  99.     }   
  100.     else  
  101.         return false;   
  102. }//~~~   
  103. /**  
  104. *校验整型最大值  
  105. *str:要校验的串。  val:比较的值  
  106. *  
  107. *返回值:  
  108. *如果为空,定义校验通过,                返回true  
  109. *如果满足条件,小于等于给定值,校验通过,返回true  
  110. *如果大于给定值,                        返回false       参考提示信息:输入值不能大于给定值!  
  111. */  
  112. function checkIntegerMaxValue(str,val)   
  113. {   
  114.     //如果为空,则通过校验   
  115.     if(str == "")   
  116.         return true;   
  117.     if(typeof(val) != "string")   
  118.         val = val + "";   
  119.     if(checkIsInteger(str) == true)   
  120.     {   
  121.         if(parseInt(str,10)<=parseInt(val,10))   
  122.             return true;   
  123.         else  
  124.             return false;   
  125.     }   
  126.     else  
  127.         return false;   
  128. }//~~~   
  129. /**  
  130. *校验整型是否为非负数  
  131. *str:要校验的串。  
  132. *  
  133. *返回值:  
  134. *如果为空,定义校验通过,返回true  
  135. *如果非负数,            返回true  
  136. *如果是负数,            返回false               参考提示信息:输入值不能是负数!  
  137. */  
  138. function isNotNegativeInteger(str)   
  139. {   
  140.     //如果为空,则通过校验   
  141.     if(str == "")   
  142.         return true;   
  143.     if(checkIsInteger(str) == true)   
  144.     {   
  145.         if(parseInt(str,10) < 0)   
  146.             return false;   
  147.         else  
  148.             return true;   
  149.     }   
  150.     else  
  151.         return false;   
  152. }//~~~   
  153. /*--------------------------------- Integer --------------------------------------*/  
  154. /********************************** Double ****************************************/  
  155. /**  
  156. *校验字符串是否为浮点型  
  157. *返回值:  
  158. *如果为空,定义校验通过,      返回true  
  159. *如果字串为浮点型,校验通过,  返回true  
  160. *如果校验不通过,              返回false     参考提示信息:输入域不是合法的浮点数!  
  161. */  
  162. function checkIsDouble(str)   
  163. {   
  164.     //如果为空,则通过校验   
  165.     if(str == "")   
  166.         return true;   
  167.     //如果是整数,则校验整数的有效性   
  168.     if(str.indexOf(".") == -1)   
  169.     {   
  170.         if(checkIsInteger(str) == true)   
  171.             return true;   
  172.         else  
  173.             return false;   
  174.     }   
  175.     else  
  176.     {   
  177.         if(/^("-?)("d+)(.{1})("d+)$/g.test(str))   
  178.             return true;   
  179.         else  
  180.             return false;   
  181.     }   
  182. }//~~~   
  183. /**  
  184. *校验浮点型最小值  
  185. *str:要校验的串。  val:比较的值  
  186. *  
  187. *返回值:  
  188. *如果为空,定义校验通过,                返回true  
  189. *如果满足条件,大于等于给定值,校验通过,返回true  
  190. *如果小于给定值,                        返回false              参考提示信息:输入域不能小于给定值!  
  191. */  
  192. function checkDoubleMinValue(str,val)   
  193. {   
  194.     //如果为空,则通过校验   
  195.     if(str == "")   
  196.         return true;   
  197.     if(typeof(val) != "string")   
  198.         val = val + "";   
  199.     if(checkIsDouble(str) == true)   
  200.     {   
  201.         if(parseFloat(str)>=parseFloat(val))   
  202.             return true;   
  203.         else  
  204.             return false;   
  205.     }   
  206.     else  
  207.         return false;   
  208. }//~~~   
  209. /**  
  210. *校验浮点型最大值  
  211. *str:要校验的串。  val:比较的值  
  212. *  
  213. *返回值:  
  214. *如果为空,定义校验通过,                返回true  
  215. *如果满足条件,小于等于给定值,校验通过,返回true  
  216. *如果大于给定值,                        返回false       参考提示信息:输入值不能大于给定值!  
  217. */  
  218. function checkDoubleMaxValue(str,val)   
  219. {   
  220.     //如果为空,则通过校验   
  221.     if(str == "")   
  222.         return true;   
  223.     if(typeof(val) != "string")   
  224.         val = val + "";   
  225.     if(checkIsDouble(str) == true)   
  226.     {   
  227.         if(parseFloat(str)<=parseFloat(val))   
  228.             return true;   
  229.         else  
  230.             return false;   
  231.     }   
  232.     else  
  233.         return false;   
  234. }//~~~   
  235. /**  
  236. *校验浮点型是否为非负数  
  237. *str:要校验的串。  
  238. *  
  239. *返回值:  
  240. *如果为空,定义校验通过,返回true  
  241. *如果非负数,            返回true  
  242. *如果是负数,            返回false               参考提示信息:输入值不能是负数!  
  243. */  
  244. function isNotNegativeDouble(str)   
  245. {   
  246.     //如果为空,则通过校验   
  247.     if(str == "")   
  248.         return true;   
  249.     if(checkIsDouble(str) == true)   
  250.     {   
  251.         if(parseFloat(str) < 0)   
  252.             return false;   
  253.         else  
  254.             return true;   
  255.     }   
  256.     else  
  257.         return false;   
  258. }//~~~   
  259. /*--------------------------------- Double ---------------------------------------*/  
  260. /********************************** date ******************************************/  
  261. /**  
  262. *校验字符串是否为日期型  
  263. *返回值:  
  264. *如果为空,定义校验通过,           返回true  
  265. *如果字串为日期型,校验通过,       返回true  
  266. *如果日期不合法,                   返回false    参考提示信息:输入域的时间不合法!(yyyy-MM-dd)  
  267. */  
  268. function checkIsValidDate(str)   
  269. {   
  270.     //如果为空,则通过校验   
  271.     if(str == "")   
  272.         return true;   
  273.     var pattern = /^(("d{4})|("d{2}))-("d{1,2})-("d{1,2})$/g;   
  274.     if(!pattern.test(str))   
  275.         return false;   
  276.     var arrDate = str.split("-");   
  277.     if(parseInt(arrDate[0],10) < 100)   
  278.         arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";   
  279.     var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);   
  280.     if(date.getYear() == arrDate[0]   
  281.        && date.getMonth() == (parseInt(arrDate[1],10) -1)+""  
  282.        && date.getDate() == arrDate[2])   
  283.         return true;   
  284.     else  
  285.         return false;   
  286. }//~~~   
  287. /**  
  288. *校验两个日期的先后  
  289. *返回值:  
  290. *如果其中有一个日期为空,校验通过,          返回true  
  291. *如果起始日期早于等于终止日期,校验通过,   返回true  
  292. *如果起始日期晚于终止日期,                 返回false    参考提示信息: 起始日期不能晚于结束日期。  
  293. */  
  294. function checkDateEarlier(strStart,strEnd)   
  295. {   
  296.     if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)   
  297.         return false;   
  298.     //如果有一个输入为空,则通过检验   
  299.     if (( strStart == "" ) || ( strEnd == "" ))   
  300.         return true;   
  301.     var arr1 = strStart.split("-");   
  302.     var arr2 = strEnd.split("-");   
  303.     var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);   
  304.     var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);   
  305.     if(arr1[1].length == 1)   
  306.         arr1[1] = "0" + arr1[1];   
  307.     if(arr1[2].length == 1)   
  308.         arr1[2] = "0" + arr1[2];   
  309.     if(arr2[1].length == 1)   
  310.         arr2[1] = "0" + arr2[1];   
  311.     if(arr2[2].length == 1)   
  312.         arr2[2]="0" + arr2[2];   
  313.     var d1 = arr1[0] + arr1[1] + arr1[2];   
  314.     var d2 = arr2[0] + arr2[1] + arr2[2];   
  315.     if(parseInt(d1,10) > parseInt(d2,10))   
  316.        return false;   
  317.     else  
  318.        return true;   
  319. }//~~~   
  320. /*--------------------------------- date -----------------------------------------*/  
  321. /********************************** email *****************************************/  
  322. /**  
  323. *校验字符串是否为email型  
  324. *返回值:  
  325. *如果为空,定义校验通过,           返回true  
  326. *如果字串为email型,校验通过,      返回true  
  327. *如果email不合法,                  返回false    参考提示信息:Email的格式不正確!  
  328. */  
  329. function checkEmail(str)   
  330. {   
  331.     //如果为空,则通过校验   
  332.     if(str == "")   
  333.         return true;   
  334.     if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf('@', 0) == -1  
  335.         || str.indexOf('.', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)   
  336.         return false;   
  337.     else  
  338.         return true;   
  339. }//~~~   
  340. /*--------------------------------- email ----------------------------------------*/  
  341. /********************************** chinese ***************************************/  
  342. /**  
  343. *校验字符串是否为中文  
  344. *返回值:  
  345. *如果为空,定义校验通过,           返回true  
  346. *如果字串为中文,校验通过,         返回true  
  347. *如果字串为非中文,             返回false    参考提示信息:必须为中文!  
  348. */  
  349. function checkIsChinese(str)   
  350. {   
  351.     //如果值为空,通过校验   
  352.     if (str == "")   
  353.         return true;   
  354.     var pattern = /^(["u4E00-"u9FA5]|["uFE30-"uFFA0])*$/gi;   
  355.     if (pattern.test(str))   
  356.         return true;   
  357.     else  
  358.         return false;   
  359. }//~~~   
  360. /**  
  361.  * 计算字符串的长度,一个汉字两个字符  
  362.  */  
  363. String.prototype.realLength = function()   
  364. {   
  365.   return this.replace(/[^"x00-"xff]/g,"**").length;   
  366. }   
  367. /*--------------------------------- chinese --------------------------------------*/  
  368. /********************************** mask ***************************************/  
  369. /**  
  370. *校验字符串是否符合自定义正则表达式  
  371. *str 要校验的字串  pat 自定义的正则表达式  
  372. *返回值:  
  373. *如果为空,定义校验通过,           返回true  
  374. *如果字串符合,校验通过,           返回true  
  375. *如果字串不符合,                   返回false    参考提示信息:必须满足***模式  
  376. */  
  377. function checkMask(str,pat)   
  378. {   
  379.     //如果值为空,通过校验   
  380.     if (str == "")   
  381.         return true;   
  382.     var pattern = new RegExp(pat,"gi")   
  383.     if (pattern.test(str))   
  384.         return true;   
  385.     else  
  386.         return false;   
  387. }//~~~   
  388. /*--------------------------------- mask --------------------------------------*/  
  389. /********************************** file ***************************************/  
  390. /**  
  391.  * added by LxcJie 2004.6.25  
  392.  * 得到文件的后缀名  
  393.  * oFile为file控件对象  
  394.  */  
  395. function getFilePostfix(oFile)   
  396. {   
  397.     if(oFile == null)   
  398.         return null;   
  399.     var pattern = /(.*)".(.*)$/gi;   
  400.     if(typeof(oFile) == "object")   
  401.     {   
  402.         if(oFile.value == null || oFile.value == "")   
  403.             return null;   
  404.         var arr = pattern.exec(oFile.value);   
  405.         return RegExp.$2;   
  406.     }   
  407.     else if(typeof(oFile) == "string")   
  408.     {   
  409.         var arr = pattern.exec(oFile);   
  410.         return RegExp.$2;   
  411.     }   
  412.     else  
  413.         return null;   
  414. }   
  415. //****************************************************************   
  416. //* 名  称:DataLength   
  417. //* 功    能:计算数据的长度   
  418. //* 入口参数:fData:需要计算的数据   
  419. //* 出口参数:返回fData的长度(Unicode长度为2,非Unicode长度为1)   
  420. //*****************************************************************   
  421. function DataLength(fData)   
  422. {   
  423.     var intLength=0  
  424.     for (var i=0;i<fData.length;i++)   
  425.     {   
  426.         if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))   
  427.             intLength=intLength+2  
  428.         else  
  429.             intLength=intLength+1       
  430.     }   
  431.     return intLength   
  432. }   
  433.   
  434.   
  435. //****************************************************************   
  436. //* 名  称:DataLength   
  437. //* 功    能:计算数据的长度   
  438. //* 入口参数:fData:需要计算的数据   
  439. //* 出口参数:返回fData的长度(Unicode长度为2,非Unicode长度为1)   
  440. //*****************************************************************   
  441. function DataLength(fData)   
  442. {   
  443.     var intLength=0  
  444.     for (var i=0;i<fData.length;i++)   
  445.     {   
  446.         if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))   
  447.             intLength=intLength+2  
  448.         else  
  449.             intLength=intLength+1       
  450.     }   
  451.     return intLength   
  452. }    
  453.   
  454. //****************************************************************   
  455. //* 名  称:IsEmpty   
  456. //* 功    能:判断是否为空   
  457. //* 入口参数:fData:要检查的数据   
  458. //* 出口参数:True:空                                 
  459. //*           False:非空   
  460. //*****************************************************************   
  461. function IsEmpty(fData)   
  462. {   
  463.     return ((fData==null) || (fData.length==0) )   
  464. }    
  465.   
  466.   
  467. //****************************************************************   
  468. //* 名  称:IsDigit   
  469. //* 功    能:判断是否为数字   
  470. //* 入口参数:fData:要检查的数据   
  471. //* 出口参数:True:是0到9的数字                                 
  472. //*           False:不是0到9的数字    
  473. //*****************************************************************   
  474. function IsDigit(fData)   
  475. {   
  476.     return ((fData>="0") && (fData<="9"))   
  477. }    
  478.   
  479.   
  480. //****************************************************************   
  481. //* 名  称:IsInteger   
  482. //* 功    能:判断是否为正整数   
  483. //* 入口参数:fData:要检查的数据   
  484. //* 出口参数:True:是整数,或者数据是空的                               
  485. //*           False:不是整数   
  486. //*****************************************************************   
  487. function IsInteger(fData)   
  488. {   
  489.     //如果为空,返回true   
  490.     if (IsEmpty(fData))   
  491.         return true  
  492.     if ((isNaN(fData)) || (fData.indexOf(".")!=-1) || (fData.indexOf("-")!=-1))   
  493.         return false       
  494.        
  495.     return true       
  496. }    
  497.   
  498. //****************************************************************   
  499. //* 名  称:IsEmail   
  500. //* 功    能:判断是否为正确的Email地址   
  501. //* 入口参数:fData:要检查的数据   
  502. //* 出口参数:True:正确的Email地址,或者空                                 
  503. //*           False:错误的Email地址   
  504. //*****************************************************************   
  505. function IsEmail(fData)   
  506. {   
  507.     if (IsEmpty(fData))   
  508.         return true  
  509.     if (fData.indexOf("@")==-1)   
  510.         return false  
  511.     var NameList=fData.split("@");   
  512.     if (NameList.length!=2)   
  513.         return false     
  514.     if (NameList[0].length<1 )   
  515.         return false      
  516.     if (NameList[1].indexOf(".")<=0)   
  517.         return false    
  518.     if (fData.indexOf("@")>fData.indexOf("."))    
  519.  return false  
  520.     if (fData.indexOf(".")==fData.length-1)   
  521.  return false  
  522.        
  523.     return true       
  524. }    
  525.   
  526. //****************************************************************   
  527. //* 名  称:IsPhone   
  528. //* 功    能:判断是否为正确的电话号码(可以含"()"、"()"、"+"、"-"和空格)   
  529. //* 入口参数:fData:要检查的数据   
  530. //* 出口参数:True:正确的电话号码,或者空                                 
  531. //*           False:错误的电话号码   
  532. //* 错误信息:   
  533. //*****************************************************************   
  534. function IsPhone(fData)   
  535. {   
  536.     var str;   
  537.     var fDatastr="";   
  538.     if (IsEmpty(fData))   
  539.         return true  
  540.     for (var i=0;i<fData.length;i++)   
  541.     {   
  542.         str=fData.substring(i,i+1);   
  543.         if (str!="(" && str!=")" && str!="(" && str!=")" && str!="+" && str!="-" && str!=" ")   
  544.            fDatastr=fDatastr+str;   
  545.     }     
  546.     //alert(fDatastr);     
  547.     if (isNaN(fDatastr))   
  548.         return false    
  549.     return true       
  550. }    
  551.   
  552. //****************************************************************   
  553. //* 名  称:IsPlusNumeric   
  554. //* 功    能:判断是否为正确的正数(可以含小数部分)   
  555. //* 入口参数:fData:要检查的数据   
  556. //* 出口参数:True:正确的正数,或者空                                 
  557. //*           False:错误的正数   
  558. //* 错误信息:   
  559. //*****************************************************************   
  560. function IsPlusNumeric(fData)   
  561. {   
  562.     if (IsEmpty(fData))   
  563.         return true  
  564.     if ((isNaN(fData)) || (fData.indexOf("-")!=-1))   
  565.         return false    
  566.     return true       
  567. }    
  568.   
  569. //****************************************************************   
  570. //* 名  称:IsNumeric   
  571. //* 功    能:判断是否为正确的数字(可以为负数,小数)   
  572. //* 入口参数:fData:要检查的数据   
  573. //* 出口参数:True:正确的数字,或者空                                 
  574. //*           False:错误的数字   
  575. //* 错误信息:   
  576. //*****************************************************************   
  577. function IsNumeric(fData)   
  578. {   
  579.     if (IsEmpty(fData))   
  580.         return true  
  581.     if (isNaN(fData))   
  582.         return false  
  583.            
  584.     return true       
  585. }    
  586.   
  587.   
  588. //****************************************************************   
  589. //* 名  称:IsIntegerInRange   
  590. //* 功    能:判断一个数字是否在指定的范围内   
  591. //* 入口参数:fInput:要检查的数据   
  592. //*           fLower:检查的范围下限,如果没有下限,请用null   
  593. //*           fHigh:检查的上限,如果没有上限,请用null   
  594. //* 出口参数:True:在指定的范围内                                 
  595. //*           False:超出指定范围   
  596. //*****************************************************************   
  597. function IsIntegerInRange(fInput,fLower,fHigh)   
  598. {   
  599.     if (fLower==null)   
  600.         return (fInput<=fHigh)   
  601.     else if (fHigh==null)   
  602.         return (fInput>=fLower)    
  603.     else            
  604.         return ((fInput>=fLower) && (fInput<=fHigh))   

posted on 2009-02-13 11:01  自己  阅读(613)  评论(0编辑  收藏  举报