posts - 22, comments - 8, trackbacks - 0, articles - 0
   :: 首页 ::  :: 联系 :: 聚合  :: 管理

javascript函数库

Posted on 2006-12-29 19:35 路易 阅读(156) 评论(0)  编辑  收藏 所属分类: 东方夜谭
  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 * added by LxcJie 2004.6.25
 25 * 去除多余空格函数
 26 * trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格
 27 * 用法:
 28 *     var str = "  hello ";
 29 *     str = str.trim();
 30 */

 31 String.prototype.trim  =   function ()
 32 {
 33      return   this .replace( / ( ^ [s] * ) | ([s] * $) / g,  "" );
 34 }

 35 String.prototype.lTrim  =   function ()
 36 {
 37      return   this .replace( / ( ^ [s] * ) / g,  "" );
 38 }

 39 String.prototype.rTrim  =   function ()
 40 {
 41      return   this .replace( / ([s] * $) / g,  "" );
 42 }

 43 /* ********************************* Empty ************************************* */
 44 /* *
 45 *校验字符串是否为空
 46 *返回值:
 47 *如果不为空,定义校验通过,返回true
 48 *如果为空,校验不通过,返回false               参考提示信息:输入域不能为空!
 49 */

 50 function  checkIsNotEmpty(str)
 51 {
 52      if (str.trim()  ==   "" )
 53          return   false ;
 54      else
 55          return   true ;
 56 }
// ~~~
 57 /* --------------------------------- Empty -------------------------------------- */
 58 /* ********************************* Integer ************************************ */
 59 /* *
 60 *校验字符串是否为整型
 61 *返回值:
 62 *如果为空,定义校验通过,      返回true
 63 *如果字串全部为数字,校验通过,返回true
 64 *如果校验不通过,              返回false     参考提示信息:输入域必须为数字!
 65 */

 66 function  checkIsInteger(str)
 67 {
 68      // 如果为空,则通过校验
 69      if (str  ==   "" )
 70          return   true ;
 71      if ( /^ ( -? )(d + )$ / .test(str))
 72          return   true ;
 73      else
 74          return   false ;
 75 }
// ~~~
 76 /* *
 77 *校验整型最小值
 78 *str:要校验的串。  val:比较的值
 79 *
 80 *返回值:
 81 *如果为空,定义校验通过,                返回true
 82 *如果满足条件,大于等于给定值,校验通过,返回true
 83 *如果小于给定值,                        返回false              参考提示信息:输入域不能小于给定值!
 84 */

 85 function  checkIntegerMinValue(str,val)
 86 {
 87      // 如果为空,则通过校验
 88      if (str  ==   "" )
 89          return   true ;
 90      if ( typeof (val)  !=   " string " )
 91         val  =  val  +   "" ;
 92      if (checkIsInteger(str)  ==   true )
 93      {
 94          if (parseInt(str, 10 ) >= parseInt(val, 10 ))
 95              return   true ;
 96          else
 97              return   false ;
 98     }

 99      else
100          return   false ;
101 }
// ~~~
102 /* *
103 *校验整型最大值
104 *str:要校验的串。  val:比较的值
105 *
106 *返回值:
107 *如果为空,定义校验通过,                返回true
108 *如果满足条件,小于等于给定值,校验通过,返回true
109 *如果大于给定值,                        返回false       参考提示信息:输入值不能大于给定值!
110 */

111 function  checkIntegerMaxValue(str,val)
112 {
113      // 如果为空,则通过校验
114      if (str  ==   "" )
115          return   true ;
116      if ( typeof (val)  !=   " string " )
117         val  =  val  +   "" ;
118      if (checkIsInteger(str)  ==   true )
119      {
120          if (parseInt(str, 10 ) <= parseInt(val, 10 ))
121              return   true ;
122          else
123              return   false ;
124     }

125      else
126          return   false ;
127 }
// ~~~
128 /* *
129 *校验整型是否为非负数
130 *str:要校验的串。
131 *
132 *返回值:
133 *如果为空,定义校验通过,返回true
134 *如果非负数,            返回true
135 *如果是负数,            返回false               参考提示信息:输入值不能是负数!
136 */

137 function  isNotNegativeInteger(str)
138 {
139      // 如果为空,则通过校验
140      if (str  ==   "" )
141          return   true ;
142      if (checkIsInteger(str)  ==   true )
143      {
144          if (parseInt(str, 10 <   0 )
145              return   false ;
146          else
147              return   true ;
148     }

149      else
150          return   false ;
151 }
// ~~~
152 /* --------------------------------- Integer -------------------------------------- */
153 /* ********************************* Double *************************************** */
154 /* *
155 *校验字符串是否为浮点型
156 *返回值:
157 *如果为空,定义校验通过,      返回true
158 *如果字串为浮点型,校验通过,  返回true
159 *如果校验不通过,              返回false     参考提示信息:输入域不是合法的浮点数!
160 */

161 function  checkIsDouble(str)
162 {
163      // 如果为空,则通过校验
164      if (str  ==   "" )
165          return   true ;
166      // 如果是整数,则校验整数的有效性
167      if (str.indexOf( " . " ==   - 1 )
168      {
169          if (checkIsInteger(str)  ==   true )
170              return   true ;
171          else
172              return   false ;
173     }

174      else
175      {
176          if ( /^ ( -? )(d + )(. { 1 } )(d + )$ / g.test(str))
177              return   true ;
178          else
179              return   false ;
180     }

181 }
// ~~~

只有注册用户登录后才能发表评论。


网站导航: