📄 validate.vbs
字号:
'************************************************************************************************
' 文件名: validate.vbs
' Copyright(c) 2001-2002 上海阿尔卡特网络支援系统有限公司
' 创建人 : 周秋舫
' 日 期 : 2002-05-22
' 修改历史 :
' ****年**月**日 ****** 修改内容:**************************************************
' 功能描述 : 输入数据有效性检查常用函数
' ValidateAllInput(Param) : 所有输入数据一起检查
' ValidateText(fld, fldDesp, bMandatory, iLength) : 所有输入数据一起检查
' ValidateNumber( , , , ) : 检查数字类型的输入数据
' ValidateNumeric( , , , ) : 检查整小数类型的输入数据
' ValidateDate( , , , ) : 检查日期类型的输入数据
' DeleteConfirm(str) : 删除提示,确认返回true,取消返回false
' 版 本 :
'************************************************************************************************
function ValidateAllInput(Param)
dim sParam
dim sDelimiter1, sDelimiter2, arrayInput, thisInput
dim sFld, sFldDesp, sFldValue, bMandatory, sType, iMaxlength
dim sError, i, sTemp
dim sFirstErrorFld
sFirstErrorFld = "" '' 存放第一个出错的字段,在返回前置输入焦点
sParam = Trim(Param)
if sParam = "" then exit function
sDelimiter1 = "&&&&"
sDelimiter2 = "****"
sError = ""
arrayInput = sParam.Split(sParam, sDelimiter1) '' 分隔符号
For i = 1 to UBound(arrayInput)
thisInput = arrayInput(i) '' 取出其中的一组输入信息
if UBound(thisInput) <> "5" then
ValidateAllInput = "啊噢,有效性检查的参数格式有误!"
exit function
end if
sFld = thisInput(1)
sFldDesp = thisInput(2)
Execute("sFldValue = " & sFld & ".value") '' 取到输入框的值放入sFldValue局部变量
bMandatory = thisInput(3)
sType = LCase(thisInput(4))
iMaxlength = thisInput(5)
select case sType
case "text"
sTemp = ValidateText(sFld, sFldDesp, bMandatory, iMaxlength)
case "number"
sTemp = ValidateNumber(sfld, sFldDesp, bMandatory, iMaxlength)
case "numeric"
sTemp = ValidateNumeric(sfld, sFldDesp, bMandatory, iMaxlength)
case "date"
sTemp = ValidateDate(sfld, sFldDesp, bMandatory)
case else
Execute( sFld & ".focus()")
ValidateAllInput = "啊噢," & sFldDesp & " 有效性检查的参数格式有误!"
exit function
end select
if sTemp <> "" then
if sFirstErrorFld = "" then sFirstErrorFld = sFld '' 作为第一个出错的字段保存起来
if sError = "" then sError = sError & vbTab & vbLF '' 加上tab缩进符和换行符
sError = sError & sTemp
end if
Next
if sError <> "" then
Execute( sFirstErrorFld & ".focus()")
ValidateAllInput = "错误信息如下:" & vbLF & sError
end if
end function
function ValidateText( psFld, psFldDesp, pbMandatory, piMaxlength)
' msgbox "here"
' ValidateText = ""
' exit function
dim sFld, sFldDesp, sFldValue, bMandatory, iMaxlength
sFld = psFld
sFldDesp = psFldDesp
bMandatory = pbMandatory
iMaxlength = piMaxlength
Execute("sFldValue = " & sFld & ".value") '' 取到输入框的值放入sFldValue局部变量
ValidateText = "" '' 函数返回值置为0;即成功
'' 首先将输入信息清空前后空格后判断是否为空,如果为空,且该项为必须输入项时,报错返回
if Trim(sFldValue) = "" and bMandatory then
Execute( sFld & ".focus()")
ValidateText = "对不起,请输入" & sFldDesp & " !"
exit function
end if
'' 文本类型的输入信息长度不能超过最大长度,如果最大长度为0,则表示不需要检查最大长度
if iMaxlength > 0 and Len(sFldValue) > iMaxlength then
Execute( sFld & ".focus()")
ValidateText = "对不起,您输入的" & sFldDesp & " 信息超过了最大长度 " & iMaxlength & "!"
exit function
end if
ValidateText = ""
end function
function ValidateNumber( psFld, psFldDesp, pbMandatory, piMaxlength)
dim sFld, sFldDesp, sFldValue, bMandatory, iMaxlength
sFld = CStr(psFld)
sFldDesp = CStr(psFldDesp)
bMandatory = CBool(pbMandatory)
iMaxlength = CInt(piMaxlength)
Execute("sFldValue = " & sFld & ".value") '' 取到输入框的值放入sFldValue局部变量
'' 首先将输入信息清空前后空格后判断是否为空,如果为空,且该项为必须输入项时,报错返回
if Trim(sFldValue) = "" and bMandatory then
Execute( sFld & ".focus()")
ValidateNumber = "对不起,请输入" & sFldDesp & "!"
exit function
end if
'' 检查数字类型的输入信息是否有效(是否均为数字)
if Not IsNumeric(sFldValue) then
Execute( sFld & ".focus()")
ValidateNumber = "对不起," & sFldDesp & "必须由0-9组成!"
exit function
end if
'' 检查数字类型的输入信息是否有效(是否均为数字)
dim re
Set re = New RegExp
re.Global = True
re.IgnoreCase = True
re.Pattern = "[0-9]" '' 匹配多个0-9字符
if Not re.Test(sFldValue) then
Execute( sFld & ".focus()")
Execute(sFld & ".select()")
ValidateNumber = "对不起," & sFldDesp & "必须由0-9组成!"
exit function
end if
Set re = nothing
'' 输入信息长度不能超过最大长度
if iMaxlength > 0 and Len(sFldValue) > iMaxlength then
Execute( sFld & ".focus()")
ValidateNumber = "对不起,您输入的" & sFldDesp & "信息超过了最大长度" & iMaxlength & "!"
exit function
end if
ValidateNumber = ""
end function
function ValidateNumeric( psFld, psFldDesp, pbMandatory, piMaxlength )
dim sFld, sFldDesp, sFldValue, bMandatory, iMaxlength
sFld = CStr(psFld)
sFldDesp = CStr(psFldDesp)
bMandatory = CBool(pbMandatory)
iMaxlength = CInt(piMaxlength)
Execute("sFldValue = " & sFld & ".value") '' 取到输入框的值放入sFldValue局部变量
'' 首先将输入信息清空前后空格后判断是否为空,如果为空,且该项为必须输入项时,报错返回
if Trim(sFldValue) = "" and bMandatory then
Execute( sFld & ".focus()")
ValidateDate = "对不起,请输入" & sFldDesp & "!"
exit function
end if
'' 检查数字类型的输入信息是否有效(是否均为数字)
if Not IsNumeric(sFldValue) then
Execute( sFld & ".focus()")
ValidateDate = "对不起," & sFldDesp & "的格式不正确,应为整数或小数数字!"
exit function
end if
ValidateNumeric = ""
end function
function ValidateDate( psFld, psFldDesp, pbMandatory, piMaxlength )
dim sFld, sFldDesp, sFldValue, bMandatory, iMaxlength
sFld = CStr(psFld)
sFldDesp = CStr(psFldDesp)
bMandatory = CBool(pbMandatory)
iMaxlength = CInt(piMaxlength)
Execute("sFldValue = " & sFld & ".value") '' 取到输入框的值放入sFldValue局部变量
'' 首先将输入信息清空前后空格后判断是否为空,如果为空,且该项为必须输入项时,报错返回
if Trim(sFldValue) = "" and bMandatory then
Execute( sFld & ".focus()")
ValidateDate = "对不起,请输入" & sFldDesp & "!"
exit function
end if
'' 检查数字类型的输入信息是否有效(是否均为数字)
if Not IsDate(sFldValue) then
Execute( sFld & ".focus()")
ValidateDate = "对不起," & sFldDesp & "的日期格式不正确!"
exit function
else
Execute( sFld & ".value=CDate(sFldValue)")
end if
ValidateDate = ""
end function
function ValidateIdentityCard(psFld, psFldDesp, pbMandatory) '' 身份证输入数据的有效性检查
dim sFld, sFldDesp, sFldValue, bMandatory
sFld = CStr(psFld)
sFldDesp = Cstr(psFldDesp)
bMandatory = CBool(pbMandatory)
Execute("sFldValue = " & sFld & ".value") '' 取到输入框的值放入sFldValue局部变量
dim re
Set re = New RegExp
re.Global = True
re.IgnoreCase = True
re.Pattern = "(\d{15})|(\d{18})" '' 15或18个数字组成
if Not Test(sFldValue) then
Execute( sFld & ".focus()")
ValidateIdentityCard = "对不起," & sFldDesp & "长度应为15或18位,且全部由数字组成!"
exit function
end if
Set re = nothing
ValidateIdentityCard = ""
end function
function ValidateEmail(psFld, psFldDesp, pbMandatory, piMaxlength)
dim sFld, sFldDesp, sFldValue, bMandatory, iMaxlength
sFld = CStr(psFld)
sFldDesp = CStr(psFldDesp)
bMandatory = CBool(pbMandatory)
Execute("sFldValue = " & sFld & ".value") '' 取到输入框的值放入sFldValue局部变量
iMaxlength = CInt(piMaxlength)
dim re
Set re = New RegExp
re.Global = True
re.IgnoreCase = True
re.Pattern = ""
if Not re.Text(sFldValue) then
Execute( sFld & ".focus()")
ValidateEmail = "对不起," & sFldDesp & "的格式不对!"
exit function
end if
Set re = nothing
ValidateEmail = ""
end function
function ValidatePercent(psFld, psFldDesp)
ValidatePercent = ""
dim sFld, sFldDesp, sFldValue
sFld = CStr(psFld)
sFldDesp = CStr(psFldDesp)
Execute("sFldValue = " & sFld & ".value")
' '' 首先将输入信息清空前后空格后判断是否为空,如果为空,且该项为必须输入项时,报错返回
if Trim(sFldValue) = "" then
Execute( sFld & ".value=""""")
Execute( sFld & ".focus()")
ValidatePercent = "对不起,请输入" & sFldDesp & " !"
exit function
end if
'' 检查数字类型的输入信息是否有效(是否均为数字)
If Not IsNumeric(sFldValue) then
Execute(sFld & ".focus()")
Execute(sFld & ".select()")
ValidatePercent = "对不起," & sFldDesp & "应为1~100之间的数字!"
exit function
end If
'' 检查输入数据的范围是否在0到100之间
sFldValue = CInt(sFldValue)
Execute(sFld & ".value = " & sFldValue)
if sFldValue < 0 or sFldValue > 100 then
Execute(sFld & ".focus()")
Execute(sFld & ".select()")
ValidatePercent = "对不起," & sFldDesp & "必须大于0,并且小于等于100!"
exit function
end if
'' 检查数字类型的输入信息是否有效(是否均为数字)
Dim regEx, retVal '' 定义正则表达式变量
Set regEx = New RegExp '' 创建正则表达式
regEx.Pattern = "^[0-9]" '' 设置模式
regEx.IgnoreCase = False '' 设置大小写无关
regEx.Global = True
if Not regEx.Test(sFldValue) then '' 执行测试
Execute(sFld & ".focus()")
Execute(sFld & ".select()")
ValidatePercent = "对不起," & sFldDesp & "应为1~100之间的数字!"
exit function
end if
end function
function ValidateIP(psFld, psFldDesp, bMandatory)
dim sFld, sFldDesp, sFldValue
dim i, n, sError
dim sFormatError : sFormatError = "对不起," & psFldDesp & "错,格式请参照***.***.***.***,其中***表示0~255之间的数字!"
sFld = CStr(psFld)
sFldDesp = CStr(psFldDesp)
Execute("sFldValue = " & sFld & ".value")
'' 首先将输入信息清空前后空格后判断是否为空,如果为空,且该项为必须输入项时,报错返回
if bMandatory and Trim(sFldValue) = "" then
Execute( sFld & ".value=""""")
Execute( sFld & ".focus()")
ValidateIP = "对不起,请输入" & sFldDesp & " !"
exit function
end if
dim arrayNum
arrayNum = split(sFldValue, ".") ' 取到以点号分隔的四个字段
if (UBound(arrayNum) - LBound(arrayNum) + 1) <> 4 then
Execute(sFld & ".focus()")
Execute(sFld & ".select()")
ValidateIP = sFormatError
exit function
end if
'' 检查每个字段信息是否有效(是否为0到255之间的数字)
sError = ""
for i = 0 to 3
n = arrayNum(i)
if Not IsNumeric(n) then
Execute(sFld & ".focus()")
Execute(sFld & ".select()")
ValidateIP = sFormatError
exit function
end if
n = CInt(n)
if n < 0 or n > 255 then
Execute(sFld & ".focus()")
Execute(sFld & ".select()")
ValidateIP = sFormatError
exit function
end if
next
end function
'************************************************************************************************
' 函数名 : DeleteConfirm
' 输 入 : str : 提示用户要删除的信息
' 输 出 : 确认删除返回true,取消删除返回false
' 功能描述: 确认删除
' 调用模块:
' 作 者 : 周秋舫
' 日 期 : 2002-06-07
' 版 本 :
'************************************************************************************************
function DeleteConfirm(str)
'' vbOKCancel, 1
'' vbExclamation, 48
'' vbDefaultButton2, 256
dim iRtn
iRtn = msgbox("您确认要删除" & str & "吗?", 305, "删除确认")
if iRtn = vbOK then
DeleteConfirm = true
elseif iRtn = vbCancel then
DeleteConfirm = false
end if
end function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -