vb中确保最后2个字符是 MB ,其余的都是数字字符

来源:百度知道 编辑:UC知道 时间:2024/06/27 07:35:18
用LOSTFOCUS事件进行

是Textbox吗?
Private Sub Text1_LostFocus()
Dim oRegExp
Set oRegExp = CreateObject("VBScript.RegExp")
oRegExp.Global = True
oRegExp.IgnoreCase = True
oRegExp.Pattern = "^[0-9]+MB$"
If Not oRegExp.Test(Text1.Text) Then
MsgBox "输入错误"
Text1.SetFocus
End If
End Sub

普通的校验方法:
Private Sub Text1_LostFocus()
strText = Text1.Text
If Len(strText) < 3 Then
GoTo ErrHandle
End If
strLast = Mid(strText, Len(strText) - 1, 2)
If strLast <> "MB" Then
GoTo ErrHandle
End If
strFirst = Mid(strText, 1, Len(strText) - 2)
For i = 1 To Len(strFirst)
If Not IsNumeric(Mid(strFirst, i, 1)) Then
GoTo ErrHandle
End If
Next
Exit Sub
ErrHandle:
MsgBox "输入错误"
Text1.SetFocus
End