Public Function FilterInput(ByRef objInputBox As Object, _
ByVal intKeyAscii As Integer, _
Optional ByVal udtInputType As enumInputType = itNumeric, _
Optional ByVal strKeyCodeRange As String = "0-9", _
Optional ByVal blnSmallNumber As Boolean = False, _
Optional ByVal intMaxLength As Integer = 0, _
Optional ByVal blnShowMsg As Boolean = False _
) As Integer
'************************************************************************
'*
'* ¹¦ÄÜ£ºÏÞÖÆÊäÈ룬¹ýÂË·Ç·¨×Ö·û/°´¼ü£¬Ö§³ÖÊý×Ö¡¢Îı¾¡¢ÈÕÆÚ¡¢Ê±¼ä¡¢ÓÊÕþ±àÂë¡¢Éí·ÝÖ¤µÈ¡£
'* ÊäÈ룺objInputBox - ÊäÈë¶ÔÏó£¨Ö§³ÖTextBox¡¢ComboBoxµÈ£©
'* intKeyAscii - ÊäÈë×Ö·û/°´¼ü
'* udtInputType(¿ÉÑ¡) - ÊäÈëÀàÐÍ
'* strKeyCodeRange(¿ÉÑ¡) - ÔÊÐí×Ö·û·¶Î§
'* blnSmallNumber(¿ÉÑ¡) - ÊÇ·ñÔÊÐíСÊýµã
'* intMaxLength(¿ÉÑ¡) - ÊäÈ볤¶È
'* blnShowMsg(¿ÉÑ¡) - ÊÇ·ñÏÔʾÌáʾ
'* ·µ»Ø£ºÈç¹ûÊäÈëΪºÏ·¨×Ö·û£¬Ö±½Ó·µ»Ø¸ÃÖµ£¬·´Ö®£¬·µ»Ø¿ÕÖµ(0)
'*
'* Ô¼¶¨£º·¶Î§·ûºÅΪ¡°-¡±£¬·Ö¸ô·ûºÅΪ¡°|¡±£¬ºÏ·¨Àý×Ó£º "a-z|.|0-9|,|A-Z|~"
'*
'* ×îºóÐ޸ģºUnruled Boy @ 1/12/2002
'*
'************************************************************************* Dim o_strRet() As String
Dim o_strText As String
Dim o_strRange As String
Dim o_intItems As Integer
Dim o_intRet As Integer
Dim o_intKeyAscii As Integer
'Dim o_blnRet As Boolean
Dim o_udtErrorType As enumErrorType
o_udtErrorType = etDefault '³õʼ»¯´íÎó£ºÎÞ
With objInputBox
Select Case TypeName(objInputBox)
Case "TextBox", "RichTextBox"
If .MaxLength > 0 Then 'Èç¹ûÓÐ×î´ó³¤¶ÈÏÞÖÆ£¬Ê¹ÓÃȱʡ
If intMaxLength = 0 Then
intMaxLength = .MaxLength
Else
If intMaxLength > .MaxLength Then
intMaxLength = .MaxLength
Else
End If
End If
Else
End If
Case Else
End Select
o_strText = .Text
End With
If intMaxLength > 0 And Len(o_strText) >= intMaxLength Then
Select Case intKeyAscii
Case vbKeyDelete, vbKeyBack 'ɾ³ýÓë»ØÍË
o_intKeyAscii = intKeyAscii
Case Else
Select Case TypeName(objInputBox)
Case "TextBox", "RichTextBox"
If objInputBox.SelLength > 0 Then 'Èç¹ûÓÐÑ¡ÖÐÎı¾
o_intKeyAscii = intKeyAscii
Else
Beep
o_udtErrorType = etMaxLength
o_intKeyAscii = 0
End If
Case Else
Beep
o_udtErrorType = etMaxLength
o_intKeyAscii = 0
End Select
End Select
Else
Select Case intKeyAscii
Case vbKeyDelete, vbKeyBack 'ɾ³ýÓë»ØÍË
o_intKeyAscii = intKeyAscii
Case vbKeyDecimal, 190 'Êý×Ö£ºÐ¡Êýµã
If udtInputType = itNumeric And blnSmallNumber Then
o_intKeyAscii = intKeyAscii
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Case vbKeySubtract, vbKeyDivide, 45 'ÈÕÆÚ×Ö·û£º-¡¢/
If udtInputType = itDate Then
o_intKeyAscii = intKeyAscii
ElseIf udtInputType = itNumeric Then
If Len(o_strText) = 0 Then
o_intKeyAscii = intKeyAscii
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Case Asc(":") 'ʱ¼ä×Ö·û£º:
If udtInputType = itTime Then
o_intKeyAscii = intKeyAscii
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Case Else
'o_blnRet = False
o_strRange = strKeyCodeRange
If o_strRange <> vbNullString Then
o_intItems = 0
'Ñ­»·È¥µô×ó±ßºÍÓұߵķָô·ûºÅ£¨"|"£©
Do While o_intItems < Len(o_strRange)
If Left(o_strRange, 1) = "|" Then
o_strRange = Right(o_strRange, _
Len(o_strRange) - 1)
ElseIf Right(o_strRange, 1) = "|" Then
o_strRange = Left(o_strRange, _
Len(o_strRange) - 1)
Else
End If
o_intItems = o_intItems + 1
Loop
'½âÊÍ×éºÏ²ÎÊý
o_strRet() = Split(o_strRange, "|")
For o_intItems = LBound(o_strRet) To UBound(o_strRet)
o_intRet = InStr(o_strRet(o_intItems), "-")
If o_intRet <> 0 Then
If intKeyAscii >= Asc(Left(o_strRet(o_intItems), o_intRet - 1)) _
And intKeyAscii <= Asc(Right(o_strRet(o_intItems), Len(o_strRet(o_intItems)) - o_intRet)) Then
o_intKeyAscii = intKeyAscii
Exit For
End If
Else
If intKeyAscii = Val(o_strRet(o_intItems)) Then
'o_blnRet = True
o_intKeyAscii = intKeyAscii
Exit For
Else
End If
End If
Next
If o_intKeyAscii = 0 Then
o_udtErrorType = etRange
Else
End If
Else
o_intKeyAscii = intKeyAscii
End If End Select
End If
If blnShowMsg Then
Select Case o_udtErrorType
Case etRange
MsgBox "ÊäÈëµÄ[·¶Î§]²»·ûºÏÒªÇó¡£" & _
"ºÏ·¨µÄÊäÈ뷶ΧΪ£º" & _
Replace(o_strRange, "|", "£¬") & "¡£", _
vbInformation
Case etMaxLength
MsgBox "ÊäÈëµÄ[³¤¶È]²»·ûºÏÒªÇó¡£" & _
"ÒªÇóÊäÈë×î´ó³¤¶ÈΪ£º " & _
CStr(intMaxLength), vbInformation
Case etInvalid
MsgBox "ÊäÈë×Ö·û²»·ûºÏÒªÇó£¬Çë¼ì²éÄúµÄÊäÈëÊÇ·ñºÏ·¨¡£" & _
"Ìáʾ£ººÏ·¨µÄÊäÈ뷶ΧΪ£º" & _
Replace(o_strRange, "|", "£¬") & "¡£", _
vbInformation
Case Else
End Select
Else
End If
Else
End If
FilterInput = o_intKeyAscii
Exit Function
handleError:
FilterInput = 0
If blnShowMsg Then
MsgBox "ÔÚ¹ýÂ˵Ĺý³ÌÖгöÏÖÒÔÏ´íÎó£º" & vbCrLf _
& Err.Description
Else
End If
On Error GoTo 0
End Function
ByVal curMinNum As Currency, _
ByVal curMaxNum As Currency, _
Optional ByVal intMaxLength As Integer = 0, _
Optional ByVal blnCanBeEmpty As Boolean = False, _
Optional ByVal blnShowMsg As Boolean = False _
) As enumErrorType
'********************************************
'*
'* ¹¦ÄÜ£ºÐ£ÑéÊäÈ루Êý×Ö£©
'* ÊäÈ룺objInputBox - ÊäÈë¶ÔÏó£¨Ö§³ÖTextBox¡¢ComboBoxµÈ£©
'* curMinNum - ÔÊÐí×îСֵ
'* curMaxNum - ÔÊÐí×î´óÖµ
'* intMaxLength(¿ÉÑ¡) - ÊäÈ볤¶È
'* blnCanBeEmpty(¿ÉÑ¡)- ÊÇ·ñÔÊÐí¿ÕÖµ
'* blnShowMsg(¿ÉÑ¡) - ÊÇ·ñÏÔʾÌáʾ
'* ·µ»Ø£ºÈç¹ûÊäÈëÖµºÏ·¨£¬·µ»ØTrue£¬·´Ö®£¬False
'*
'* ×îºóÐ޸ģºUnruled Boy @ 1/11/2002
'*
'********************************************
On Error GoTo handleError
Dim o_strText As String
Dim o_lngNumber As Currency
Dim o_udtErrorType As enumErrorType
o_udtErrorType = etDefault '³õʼ»¯´íÎó£ºÎÞ
With objInputBox
Select Case TypeName(objInputBox)
Case "TextBox", "RichTextBox"
If .MaxLength > 0 Then 'Èç¹ûÓÐ×î´ó³¤¶ÈÏÞÖÆ£¬Ê¹ÓÃȱʡ
If intMaxLength = 0 Then
intMaxLength = .MaxLength
Else
If intMaxLength > .MaxLength Then
intMaxLength = .MaxLength
Else
End If
End If
Else
End If
Case Else
End Select
o_strText = .Text
'¿´Ä¿Ç°³¤¶ÈÊÇ·ñ³¬Ô½ÏÞÖƳ¤¶È£¬Èç¹ûÊÇ£¬Ìáʾ£¬ÊäÈëÎÞЧ
If intMaxLength > 0 And Len(o_strText) > intMaxLength Then
o_udtErrorType = etMaxLength
Else
If o_strText <> vbNullString Then
If IsNumeric(o_strText) Then
o_lngNumber = CCur(o_strText)
If o_lngNumber < curMinNum Or o_lngNumber > curMaxNum Then
On Error Resume Next
.SetFocus
On Error GoTo 0
o_udtErrorType = etRange
Else
o_udtErrorType = etDefault
End If
Else
o_udtErrorType = etInvalid
End If
Else
If Not blnCanBeEmpty Then
o_udtErrorType = etEmpty
Else
End If
End If
End If
If o_udtErrorType <> etDefault Then 'Èç¹û½á¹ûÓÐ´í£¬¸ù¾Ý´íÎóÀàÐÍ£¬Ìáʾ
On Error Resume Next
.SetFocus
On Error GoTo 0
If blnShowMsg Then
Select Case o_udtErrorType
Case etRange
MsgBox "ÊäÈëÖµ[·¶Î§]²»·ûºÏÒªÇó¡£" & vbCrLf & _
"ÒªÇóÊäÈ뷶ΧΪ£º " & curMinNum & _
" µ½ " & curMaxNum, vbInformation
Case etMaxLength
MsgBox "ÊäÈëÖµ[³¤¶È]²»·ûºÏÒªÇó¡£" & _
"ÒªÇóÊäÈ볤¶ÈΪ£º " & _
CStr(intMaxLength), vbInformation
Case etInvalid
MsgBox "ÊäÈëÒªÇóÈ«²¿ÎªÊý×Ö(°üÀ¨¡°.¡±)¡£" & vbCrLf & _
"Çë¼ì²éÊäÈëµÄÊÇ·ñÓзÇÊý×Ö·ûºÅ¡£" _
, vbInformation
Case etEmpty
MsgBox "ÊäÈë²»ÄÜΪ¿ÕÖµ¡£" & vbCrLf & _
"Äú±ØÐë°´ÒªÇóÊäÈëÊý×Ö" _
, vbInformation
Case Else
End Select
Else
End If
Else
End If
End With
ValidateNumber = o_udtErrorType
Exit Function
handleError:
ValidateNumber = etUnknown
If blnShowMsg Then
MsgBox "ÔÚУÑéÊý×ֵĹý³ÌÖгöÏÖÒÔÏ´íÎó£º" & vbCrLf _
& Err.Description
Else
End If
On Error GoTo 0
End Function
Public Function ValidateText(ByRef objInputBox As Object, _
ByVal strForbiddenChars As String, _
Optional ByVal strSplitChar As String = "|", _
Optional ByVal strReplaceChar As String = vbNullString, _
Optional ByVal blnAuthReplace As Boolean = False, _
Optional ByVal intMaxLength As Integer = 0, _
Optional ByVal blnCanBeEmpty As Boolean = False, _
Optional ByVal blnShowMsg As Boolean = False _
) As enumErrorType
'********************************************
'*
'* ¹¦ÄÜ£ºÐ£ÑéÊäÈ루×Ö·û£©
'* ÊäÈ룺objInputBox - ÊäÈë¶ÔÏó£¨Ö§³ÖTextBox¡¢ComboBoxµÈ£©
'* strForbiddenChars - ÔÊÐí×î´óÖµ
'* strSplitChar¡¡¡¡¡¡ - ·Ö¸î×Ö·û
'* strReplaceChar(¿ÉÑ¡) - Ìæ»»×Ö·û
'* blnAuthReplace(¿ÉÑ¡) - ÊÇ·ñ×Ô¶¯Ìæ»»×Ö·û
'* intMaxLength(¿ÉÑ¡) - ÊäÈ볤¶È
'* blnCanBeEmpty(¿ÉÑ¡) - ÊÇ·ñÔÊÐí¿ÕÖµ
'* blnShowMsg(¿ÉÑ¡) - ÊÇ·ñÏÔʾÌáʾ
'*
'* ·µ»Ø£ºÈç¹ûÊäÈëÖµºÏ·¨£¬·µ»ØTrue£¬·´Ö®£¬False
'*
'* ×îºóÐ޸ģºUnruled Boy @ 1/11/2002
'*
'********************************************
On Error GoTo handleError
Dim o_strRet() As String
Dim o_strText As String
Dim o_intItems As Integer
Dim o_intItems2 As Integer
Dim o_intPos As Integer
Dim o_intPos2 As Integer
Dim o_intLen As Integer
Dim o_udtErrorType As enumErrorType
o_udtErrorType = etDefault '³õʼ»¯´íÎó£ºÎÞ
With objInputBox
Select Case TypeName(objInputBox)
Case "TextBox", "RichTextBox"
If .MaxLength > 0 Then 'Èç¹ûÓÐ×î´ó³¤¶ÈÏÞÖÆ£¬Ê¹ÓÃȱʡ
If intMaxLength = 0 Then
intMaxLength = .MaxLength
Else
If intMaxLength > .MaxLength Then
intMaxLength = .MaxLength
Else
End If
End If
Else
End If
Case Else
End Select
o_strText = .Text
'¿´Ä¿Ç°³¤¶ÈÊÇ·ñ³¬Ô½ÏÞÖƳ¤¶È£¬Èç¹ûÊÇ£¬Ìáʾ£¬ÊäÈëÎÞЧ
If intMaxLength > 0 And Len(o_strText) > intMaxLength Then
o_udtErrorType = etMaxLength
Else
If o_strText <> vbNullString And strForbiddenChars <> vbNullString Then
o_intItems2 = 0
o_intPos2 = Len(o_strText)
o_intLen = 0
o_strRet() = Split(strForbiddenChars, strSplitChar)
For o_intItems = LBound(o_strRet) To UBound(o_strRet)
o_intPos = InStr(o_strText, o_strRet(o_intItems))
If o_intPos > 0 And o_intPos < o_intPos2 Then 'µÚÒ»¸ö³öÏÖλÖÃ
o_intPos2 = o_intPos
o_intLen = Len(o_strRet(o_intItems))
End If
If o_intPos <> 0 Then
If blnAuthReplace Then
o_strText = Replace(o_strText, _
o_strRet(o_intItems), _
strReplaceChar)
o_udtErrorType = etModified
Else
o_udtErrorType = etInvalid
End If
Else
o_intItems2 = o_intItems2 + 1
End If
Next
If blnAuthReplace Then
.Text = o_strText
Else
.SelStart = o_intPos2 - 1
.SelLength = o_intLen
End If
If o_intItems2 > UBound(o_strRet) Then
o_udtErrorType = etDefault
Else
End If
Else
If Not blnCanBeEmpty Then
o_udtErrorType = etEmpty
Else
End If
End If
End If
On Error Resume Next
.SetFocus
On Error GoTo 0
If blnShowMsg Then
Select Case o_udtErrorType
Case etInvalid
MsgBox "ÊäÈë²»·ûºÏÒªÇó¡£" & vbCrLf & _
"ÒÔÏÂ×Ö·û²»ÄÜ´æÔÚ£º " & vbCrLf & _
Join(o_strRet, "£¬") & "¡£" _
, vbInformation
Case etModified
MsgBox "ÊäÈë²»·ûºÏÒªÇó¡£" & vbCrLf & _
"ÒÔÏÂ×Ö·û²»ÄÜ´æÔÚ£º " & _
Join(o_strRet, "£¬") & "¡£" & vbCrLf & _
IIf(blnAuthReplace, vbCrLf & _
"¸Ã×Ö·ûÒѾ­±»×Ô¶¯¸üÕý¡£", vbNullString) _
, vbInformation
Case etEmpty
MsgBox "ÊäÈë²»·ûºÏÒªÇó¡£" & vbCrLf & _
"²»ÄÜΪ¿Õ£¬Äú±ØÐë°´ÒªÇóÊäÈëÎÄ×Ö¡£" _
, vbInformation
End Select
Else
End If
Else
End If
End With
ValidateText = o_udtErrorType
Exit Function
handleError:
ValidateText = etUnknown
If blnShowMsg Then
MsgBox "ÔÚУÑéÎı¾µÄ¹ý³ÌÖгöÏÖÒÔÏ´íÎó£º" & vbCrLf _
& Err.Description
Else
End If
On Error GoTo 0
End Function
Public Function ValidatePostCode(ByRef objInputBox As Object, _
Optional ByVal blnCanBeEmpty As Boolean = False, _
Optional ByVal blnShowMsg As Boolean = False _
) As enumErrorType
'********************************************
'*
'* ¹¦ÄÜ£ºÐ£ÑéÊäÈ루ÓÊÕþ±àÂ룩
'* ÊäÈ룺objInputBox - ÊäÈë¶ÔÏó£¨Ö§³ÖTextBox¡¢ComboBoxµÈ£©
'* blnCanBeEmpty(¿ÉÑ¡) - ÊÇ·ñÔÊÐí¿ÕÖµ
'* blnShowMsg(¿ÉÑ¡) - ÊÇ·ñÏÔʾÌáʾ
'*
'* ·µ»Ø£ºÈç¹ûÊäÈëÖµºÏ·¨£¬·µ»ØTrue£¬·´Ö®£¬False
'*
'* ×îºóÐ޸ģºUnruled Boy @ 1/13/2002
'*
'********************************************
On Error GoTo handleError Dim o_strText As String
Dim o_udtErrorType As enumErrorType
o_udtErrorType = etDefault '³õʼ»¯´íÎó£ºÎÞ
With objInputBox
o_strText = .Text
'¿´Ä¿Ç°³¤¶ÈÊÇ·ñ³¬Ô½ÏÞÖƳ¤¶È£¬Èç¹ûÊÇ£¬Ìáʾ£¬ÊäÈëÎÞЧ
If Len(o_strText) > mc_intPostCodeLength Then
o_udtErrorType = etMaxLength
Else
If o_strText <> vbNullString Then
If IsNumeric(o_strText) Then
If Len(o_strText) = mc_intPostCodeLength Then
'
'
'
o_udtErrorType = etDefault
Else
o_udtErrorType = etInvalid
End If
Else
o_udtErrorType = etRange
End If
Else
If Not blnCanBeEmpty Then
o_udtErrorType = etEmpty
Else
End If
End If
End If
If o_udtErrorType <> etDefault Then 'Èç¹û½á¹ûÓÐ´í£¬¸ù¾Ý´íÎóÀàÐÍ£¬Ìáʾ
On Error Resume Next
.SetFocus
On Error GoTo 0
If blnShowMsg Then
Select Case o_udtErrorType
Case etRange
MsgBox "ÊäÈëÖµ[·¶Î§]²»·ûºÏÒªÇó¡£" & vbCrLf & _
"ÓÊÕþ±àÂëÒªÇóÈ«²¿ÎªÊý×Ö¡£ " _
, vbInformation
Case etMaxLength
MsgBox "ÊäÈëÖµ[³¤¶È]²»·ûºÏÒªÇó¡£" & _
"ÒªÇóÊäÈ볤¶ÈΪ£º " & _
CStr(mc_intPostCodeLength), vbInformation
Case etInvalid
MsgBox "ÊäÈëµÄÓÊÕþ±àÂë²»·ûºÏÒªÇó¡£" & vbCrLf & _
"Ìáʾ£ºÓÊÕþ±àÂë±ØÐëÈ«²¿ÎªÊý×Ö£¬" & _
"¶øÇÒ³¤¶È±ØÐëΪ" & _
CStr(mc_intPostCodeLength) & "λ¡£¡£" _
, vbInformation
Case etEmpty
MsgBox "ÊäÈë²»·ûºÏÒªÇó¡£" & vbCrLf & _
"ÓÊÕþ±àÂë²»ÄÜΪ¿Õ£¬Äú±ØÐë°´ÒªÇóÊäÈë¡£" _
, vbInformation
End Select
Else
End If
Else
End If
End With
ValidatePostCode = o_udtErrorType
Exit Function
handleError:
ValidatePostCode = etUnknown
If blnShowMsg Then
MsgBox "ÔÚУÑéÓÊÕþ±àÂëµÄ¹ý³ÌÖгöÏÖÒÔÏ´íÎó£º" & vbCrLf _
& Err.Description
Else
End If
On Error GoTo 0
End Function
你可以把代码发到-文档中心-
呵呵,虽然麻烦点,没有转码工具的时候用来在big5/unicode/gb之间转码也倒有效。好长啊,enmity ...
不如upload倒某个地方然后贴url咯 :)
你提供的这个网址,不知怎么回事,FlashGet总是下的不对(100多K,打不开),但其他都好的,包括蚂蚁,ie直接下都可以,可能FlashGet有Bug