private sub text1_lostfocus() if trim(text1.text)<>"" then if not isnumeric(text1.text) then msgbox "请输入数值型数据!",48,"提示" text1.setfocus exit sub end if end if end sub
Isnumeric(exp)如果为数字,返回True,不是数字,返回False
Public Function limitnumber(ByVal intVal As Integer) As Integer '-------------------1------------------- '目 的:只允许在文本框内输入数字、退格、删除及回车键和点 '输 入:ByVal IntVal As Integer,任意的键值 '被传递值:无 '返 回 值:过滤后的键值 '输 出:无 '注 解: '用 法:在文本框的KeyPress事件中输入KeyAscii = LimitNumber(KeyAscii)即可 '修 订 版: '-------------------1------------------- If (intVal <> vbKeyDelete) _ And (intVal <> vbKeyBack) _ And (intVal <> 13) _ And (intVal <> 46) _ And (intVal < 48 Or intVal > 57) Then intVal = 0 End If limitnumber = intVal End Function这样不是数字和小数点根本就输不进去
屏蔽掉其他字符和中文输入法Private Sub Text1_KeyPress(KeyAscii As Integer) '只能输入一个小数点 If KeyAscii = 46 Then If Not (InStr(Text1.Text, ".") = 0 Or IsNull(InStr(Text1.Text, "."))) Then KeyAscii = 0 End If End If
'屏蔽掉其他字符和中文输入法 If KeyAscii<0 Or (KeyAscii >= 32 And KeyAscii <= 45) Or KeyAscii = 47 Or KeyAscii > 57 Then KeyAscii = 0 End If
End Sub
If KeyCode = 8 Or (KeyCode >= 48 And KeyCode <= 105) Then If KeyCode > 57 And KeyCode < 96 Then MsgBox "只能输入数字!"'输入大于9大键盘的“9”或小于小键盘的0 MsgBox "只能输入数字!" Else:MsgBox "只能输入数字!" '输入小于大键盘的“0”或大于小键盘的“9” End If
if trim(text1.text)<>"" then
if not isnumeric(text1.text) then
msgbox "请输入数值型数据!",48,"提示"
text1.setfocus
exit sub
end if
end if
end sub
'-------------------1-------------------
'目 的:只允许在文本框内输入数字、退格、删除及回车键和点
'输 入:ByVal IntVal As Integer,任意的键值
'被传递值:无
'返 回 值:过滤后的键值
'输 出:无
'注 解:
'用 法:在文本框的KeyPress事件中输入KeyAscii = LimitNumber(KeyAscii)即可
'修 订 版:
'-------------------1-------------------
If (intVal <> vbKeyDelete) _
And (intVal <> vbKeyBack) _
And (intVal <> 13) _
And (intVal <> 46) _
And (intVal < 48 Or intVal > 57) Then
intVal = 0
End If
limitnumber = intVal
End Function这样不是数字和小数点根本就输不进去
语法: 返回Boolean值,指出表达式的运算结果是否为数
IsNumeric(expression)
必需expression参数是一个变体型Variant,包含数值表达式或字符串表达式.
说明: 如果整个expression的运算结果为数字,则IsNumeric返回True;否则返回False
如果expression是日期表达式,则IsNumeric返回flash很清楚了。楼主 例子我就不写了
Isnumeric的结果是True
If KeyAscii = 46 Then
If Not (InStr(Text1.Text, ".") = 0 Or IsNull(InStr(Text1.Text, "."))) Then
KeyAscii = 0
End If
End If
'屏蔽掉其他字符和中文输入法
If KeyAscii<0 Or (KeyAscii >= 32 And KeyAscii <= 45) Or KeyAscii = 47 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
If KeyCode > 57 And KeyCode < 96 Then MsgBox "只能输入数字!"'输入大于9大键盘的“9”或小于小键盘的0
MsgBox "只能输入数字!"
Else:MsgBox "只能输入数字!" '输入小于大键盘的“0”或大于小键盘的“9”
End If