VB 文本框上的十进制点验证 [英] Decimal Dots validation on VB textbox
本文介绍了VB 文本框上的十进制点验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在按键事件下,我有一个验证输入字符的功能,这是我的代码.
Under keypress event, I have a function validating the entered characters, this is my code.
Public Function vNum2(val As Object)
Dim result As Boolean = False
Dim allowedChars As String = "0123456789." & vbBack
Try
If allowedChars.IndexOf(val) = -1 Then
result = True
End If
Catch ex As Exception
MsgBox("Error 1010xVNum2: " & ex.Message)
End Try
Return result
End Function
当我输入超过 2 个小数点时,如何验证小数点?当我再按一个点时,文本框将接收不到字符.
How do i validate a decimal when I entered more than 2 dots in decimal? When I press another dot, the textbox will not receive the character.
例如:-> 正确的条目 45.23 接收第一个点.-> 验证条目 45.2.3 将不会收到下一个点.
E.g: -> correct entry 45.23 receive the first dot. -> validating entry 45.2.3 will not receive the next dot.
推荐答案
试试这个:
Public Function vNum2(val As Object)
Dim result As Boolean = False
Try
'Dim allowedChars As String = "42.2.3"
Dim allowedChars As String = val.ToString()
'Bellow line will count how many dots are in string, if there one or none, result will be True
If allowedChars.Where(Function(dots) dots = ".").Count < 2 Then result = True
Catch ex As Exception
MsgBox("Error 1010xVNum2: " & ex.Message)
End Try
Return result
End Function
这篇关于VB 文本框上的十进制点验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文