一些经常会用到的vbscript检测函数

80酷酷网    80kuku.com

  vbscript|函数'----------------------------------------------------------
' Function Name : Length
' Function Desc : 返回字符串的实际长度, 一个汉字算2个长度
'---------------------------------------------------------
Public Function Length(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "[^\x00-\xff]"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
Length = Len(oRegExp.Replace(sInput, "**"))

Set oRegExp = Nothing

End Function

'-----------------------------------------------------------------
' Function Name : IsValidDate
' Function Desc : 判断输入是否是有效的短日期格式 - "YYYY-MM-DD"
'----------------------------------------------------------------
Public Function IsValidDate(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^\d{4}-\d{2}-\d{2}$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
If oRegExp.Test(sInput) Then
IsValidDate = IsDate(sInput)
Else
IsValidDate = False
End If

Set oRegExp = Nothing

End Function

'-------------------------------------------------------------
' Function Name : IsValidTime
' Function Desc : 判断输入是否是有效的时间格式 - "HH:MM:SS"
'--------------------------------------------------------------
Public Function IsValidTime(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^\d{2}:\d{2}:\d{2}$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
If oRegExp.Test(sInput) Then
IsValidTime = IsDate(sInput)
Else
IsValidTime = False
End If

Set oRegExp = Nothing

End Function

'---------------------------------------------------------
' Function Name : IsValidEmail
' Function Desc : 判断输入是否是有效的电子邮件
'---------------------------------------------------------
Public Function IsValidEmail(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^\w+((-\w+)|(\.\w))*\[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidEmail = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'------------------------------------------------------------
' Function Name : IsValidDatetime
' Function Desc : 判断输入是否是有效的长日期格式 - "YYYY-MM-DD HH:MM:SS"
'------------------------------------------------------------
Public Function IsValidDatetime(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
If oRegExp.Test(sInput) Then
IsValidDatetime = IsDate(sInput)
Else
IsValidDatetime = False
End If

Set oRegExp = Nothing

End Function

'----------------------------------------------------------------
' Function Name : IsValidInteger
' Function Desc : 判断输入是否是一个整数
'----------------------------------------------------------------
Public Function IsValidInteger(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^(-|\+)?\d+$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidInteger = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'-------------------------------------------------------------
' Function Name : IsValidPositiveInteger
' Function Desc : 判断输入是否是一个正整数
'-----------------------------------------------------------
Public Function IsValidPositiveInteger(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^(\+)?\d+$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidPositiveInteger = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'-------------------------------------------------------------
' Function Name : IsValidNegativeInteger
' Function Desc : 判断输入是否是一个负整数
'-------------------------------------------------------------
Public Function IsValidNegativeInteger(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^-\d+$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidNegativeInteger = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'-------------------------------------------------------
' Function Name : IsValidNumber
' Function Desc : 判断输入是否是一个数字
'------------------------------------------------------
Public Function IsValidNumber(sInput)

IsValidNumber =  IsNumeric(sInput)

End Function

'----------------------------------------------------------
' Function Name : IsValidLetters
' Function Desc : 判断输入是否是一个由 A-Z / a-z 组成的字符串
'----------------------------------------------------------
Public Function IsValidLetters(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^[a-zA-Z]+$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidLetters = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'----------------------------------------------------------
' Function Name : IsValidDigits
' Function Desc : 判断输入是否是一个由 0-9 组成的数字
'----------------------------------------------------------
Public Function IsValidDigits(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^[1-9][0-9]*$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidDigits = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'--------------------------------------------------------------
' Function Name : IsValidAlphanumeric
' Function Desc : 判断输入是否是一个由 0-9 / A-Z / a-z 组成的字符串
'---------------------------------------------------------------
Public Function IsValidAlphanumeric(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^[a-zA-Z0-9]+$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidAlphanumeric = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'-------------------------------------------------------------
' Function Name : IsValidString
' Function Desc : 判断输入是否是一个由 0-9 / A-Z / a-z / . / _ 组成的字符串
'-------------------------------------------------------------
Public Function IsValidString(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^[a-zA-Z0-9\s.\-_]+$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidString = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'-----------------------------------------------------------------
' Function Name : IsValidPostalcode
' Function Desc : 判断输入是否是一个有效的邮政编码
'----------------------------------------------------------------
Public Function IsValidPostalcode(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^\d{6}$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidPostalcode = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'-------------------------------------------------------------------
' Function Name : IsValidPhoneNo
' Function Desc : 判断输入是否是一个有效的电话号码
'-------------------------------------------------------------------
Public Function IsValidPhoneNo(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "(^0\d{2,3}\-[1-9]\d{2,7}$)|(^[1-9]\d{2,7}$)|(^\(0[1-9]{2,3}\)[1-9]\d{2,7}$)"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidPhoneNo = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

'--------------------------------------------------------------------
' Function Name : IsValidMobileNo
' Function Desc : 判断输入是否是一个有效的手机号码
'---------------------------------------------------------------------
Public Function IsValidMobileNo(sInput)

Dim oRegExp

'建立正则表达式
Set oRegExp = New RegExp

'设置模式
oRegExp.Pattern = "^0?13\d{9}$"
'设置是否区分字符大小写
oRegExp.IgnoreCase = True
'设置全局可用性
oRegExp.Global = True

'执行搜索
IsValidMobileNo = oRegExp.Test(sInput)

Set oRegExp = Nothing

End Function

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: