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

王朝vb·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

'----------------------------------------------------------

' 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 : 判断输入是否是一个有效的电

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航