Javascript检测函数

// 原作: Er@ser@CCF

//  Function Name: trim
//
 Function Description: 去除字符串的首尾的空格
//
 Creation Date: 2004-7-13 15:30
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.trim = function (){
return   this .replace( / ( ^ \s * ) | (\s * $) / g,  "" );
}

//  Function Name: ltrim
//
 Function Description: 去除字符串的左侧的空格
//
 Creation Date: 2004-7-13 9:58
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.ltrim = function ()
{
return   this .replace( / ( ^ \s * ) / g,  "" );
}

//  Function Name: rtrim
//
 Function Description: 去除字符串的右侧的空格
//
 Creation Date: 2004-7-13 15:31
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.rtrim = function ()
{
return   this .replace( / (\s * $) / g,  "" );
}

//  Function Name: len
//
 Function Description: 返回字符串的实际长度, 一个汉字算2个长度
//
 Creation Date: 2004-7-13 9:58
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.len = function ()
{
var  str = this ;
return  str.replace( / [ ^ \x00 - \xff] / g,  " ** " ).length
}

//  Function Name: isValidDate
//
 Function Description: 判断输入是否是有效的短日期格式 - "YYYY-MM-DD"
//
 Creation Date: 2004-7-13 9:58
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidDate = function ()
{
var  result = this .match( /^ (\d{ 1 , 4 })( -| \ / )(\d{ 1 , 2 })\ 2 (\d{ 1 , 2 })$ / );
if (result == null return   false ;
var  d = new  Date(result[ 1 ], result[ 3 ] - 1 , result[ 4 ]);
return  (d.getFullYear() == result[ 1 ] && d.getMonth() + 1 == result[ 3 ] && d.getDate() == result[ 4 ]);
}

//  Function Name: isValidTime
//
 Function Description: 判断输入是否是有效的时间格式 - "HH:MM:SS"
//
 Creation Date: 2004-7-13 9:58
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidTime = function ()
{
var  resule = this .match( /^ (\d{ 1 , 2 })(:) ? (\d{ 1 , 2 })\ 2 (\d{ 1 , 2 })$ / );
if  (result == null return   false ;
if  (result[ 1 ] > 24   ||  result[ 3 ] > 60   ||  result[ 4 ] > 60 return   false ;
return   true ;
}

//  Function Name: isValidEmail
//
 Function Description: 判断输入是否是有效的电子邮件
//
 Creation Date: 2004-7-13 9:59
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidEmail = function ()
{
var  result = this .match( /^ \w + (( - \w + ) | (\.\w + )) * \@[A - Za - z0 - 9 ] + ((\. |- )[A - Za - z0 - 9 ] + ) * \.[A - Za - z0 - 9 ] + $ / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidDatetime
//
 Function Description: 判断输入是否是有效的长日期格式 - "YYYY-MM-DD HH:MM:SS"
//
 Creation Date: 2004-7-13 9:59
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidDatetime = function ()
{
var  result = this .match( /^ (\d{ 1 , 4 })( -| \ / )(\d{ 1 , 2 })\ 2 (\d{ 1 , 2 }) (\d{ 1 , 2 }):(\d{ 1 , 2 }):(\d{ 1 , 2 })$ / );
if (result == null return   false ;
var  d =   new  Date(result[ 1 ], result[ 3 ] - 1 , result[ 4 ], result[ 5 ], result[ 6 ], result[ 7 ]);
return  (d.getFullYear() == result[ 1 ] && (d.getMonth() + 1 ) == result[ 3 ] && d.getDate() == result[ 4 ] && d.getHours() == result[ 5 ] && d.getMinutes() == result[ 6 ] && d.getSeconds() == result[ 7 ]);
}

//  Function Name: isValidInteger
//
 Function Description: 判断输入是否是一个整数
//
 Creation Date: 2004-7-13 10:01
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidInteger = function ()
{
var  result = this .match( /^ ( -| \ + ) ? \d + $ / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidPositiveInteger
//
 Function Description: 判断输入是否是一个正整数
//
 Creation Date: 2004-7-13 10:01
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidPositiveInteger = function ()
{
var  result = this .match( /^ \d + $ / );
if (result == null return   false ;
if (parseInt( this ) > 0 return   true ;
return   false ;
}

//  Function Name: isValidNegativeInteger
//
 Function Description: 判断输入是否是一个负整数
//
 Creation Date: 2004-7-13 10:28
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidNegativeInteger = function ()
{
var  result = this .match( /^- \d + $ / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidNumber
//
 Function Description: 判断输入是否是一个数字
//
 Creation Date: 2004-7-13 10:01
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidNumber = function ()
{
return   ! isNaN( this );
}

//  Function Name: isValidLetters
//
 Function Description: 判断输入是否是一个由 A-Z / a-z 组成的字符串
//
 Creation Date: 2004-7-13 10:10
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidLetters = function ()
{
var  result = this .match( /^ [a - zA - Z] + $ / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidDigits
//
 Function Description: 判断输入是否是一个由 0-9 组成的数字
//
 Creation Date: 2004-7-13 10:10
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidDigits = function ()
{
var  result = this .match( /^ [ 1 - 9 ][ 0 - 9 ] + $ / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidAlphanumeric
//
 Function Description: 判断输入是否是一个由 0-9 / A-Z / a-z 组成的字符串
//
 Creation Date: 2004-7-13 10:14
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidAlphanumeric = function ()
{
var  result = this .match( /^ [a - zA - Z0 - 9 ] + $ / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidString
//
 Function Description: 判断输入是否是一个由 0-9 / A-Z / a-z / . / _ 组成的字符串
//
 Creation Date: 2004-7-13 10:20
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidString = function ()
{
var  result = this .match( /^ [a - zA - Z0 - 9 \s.\ - _] + $ / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidPostalcode
//
 Function Description: 判断输入是否是一个有效的邮政编码
//
 Creation Date: 2004-7-13 10:22
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidPostalcode = function ()
{
var  result = this .match( / ( ^ [ 0 - 9 ]{ 6 }$) / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidPhoneNo
//
 Function Description: 判断输入是否是一个有效的电话号码
//
 Creation Date: 2004-7-13 10:22
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidPhoneNo = function ()
{
var  result = this .match( / ( ^ [ 0 - 9 ]{ 3 , 4 }\ - [ 0 - 9 ]{ 3 , 8 }$) | ( ^ [ 0 - 9 ]{ 3 , 8 }$) | ( ^ \([ 0 - 9 ]{ 3 , 4 }\)[ 0 - 9 ]{ 3 , 8 }$) / );
if (result == null return   false ;
return   true ;
}

//  Function Name: isValidMobileNo
//
 Function Description: 判断输入是否是一个有效的手机号码
//
 Creation Date: 2004-7-13 10:23
//
 Last Modify By: N/A
//
 Last Modify Date: N/A
String.prototype.isValidMobileNo = function ()
{
var  result = this .match( / ( ^ 0 { 0 , 1 } 13 [ 0 - 9 ]{ 9 }$) / );
if (result == null return   false ;
return   true ;
}

转载于:https://www.cnblogs.com/raymond19840709/archive/2007/12/25/1013796.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值