'引用 Microsoft VBScript Regular Expressions 5.5'test方法 Function bTest(ByVal s As String, ByVal p As String) As Boolean Dim re As RegExp Set re = New RegExp re.IgnoreCase = False re.Pattern = p bTest = re.Test(s) End Function Private Sub Command1_Click() Dim p As String p = "^[^0]\d*$|[1-9]+[.]\d*|[0][.]\d+$" If Not bTest(Text6, p) Then MsgBox "输入不合法" End If End Sub
引用 Microsoft VBScript Regular Expressions 5.5'test方法 Function bTest(ByVal s As String, ByVal p As String) As Boolean Dim re As RegExp Set re = New RegExp re.IgnoreCase = False re.Pattern = p bTest = re.Test(s) End FunctionPrivate Sub Command21_Click() Dim p As String p = "^[^0]\d*$|^[1-9]+[.]\d*$|^[0][.]\d+$" If Not bTest(Text6, p) Then MsgBox "输入不合法" End If End Sub
IsNumeric判断是否符合1
然后用CDbl强制转换为Double,判断转换前和转换后的字符串是否一样,如果不一样,则说明可能输入了0123这样的数字。
Function bTest(ByVal s As String, ByVal p As String) As Boolean
Dim re As RegExp
Set re = New RegExp
re.IgnoreCase = False
re.Pattern = p
bTest = re.Test(s)
End Function
Private Sub Command1_Click()
Dim p As String
p = "^[^0]\d*$|[1-9]+[.]\d*|[0][.]\d+$"
If Not bTest(Text6, p) Then
MsgBox "输入不合法"
End If
End Sub
Function bTest(ByVal s As String, ByVal p As String) As Boolean
Dim re As RegExp
Set re = New RegExp
re.IgnoreCase = False
re.Pattern = p
bTest = re.Test(s)
End FunctionPrivate Sub Command21_Click()
Dim p As String
p = "^[^0]\d*$|^[1-9]+[.]\d*$|^[0][.]\d+$"
If Not bTest(Text6, p) Then
MsgBox "输入不合法"
End If
End Sub
1# 的数据2个好象都没发现什么!2# 的非法数据只因为第一个字符串为 "0"这样的话 只需要判断text的第一个字符是否为 "0" 就知道该数据是否违法了要不还可以这样:在text的 keypess 事件中判断 若空 并且输入的keyascii= 48(0的asc 好象是这个吧!) then keyascii = 0这样就可以避免非法字符的出现了
2# 输入0123 就是非法的。
如何实现在TEXT中,只能输入1#样式的数字,而无法输入2#样式的数字
--------------------------If Len(Cstr(val(Text1.Text))=Len(Trim(Text1.Text)) Then
'是合法数字
Else
'是非法数字
End If
随手写的,你测试一下