textbox只能输入数字
限制textbox输入的只能是数字,包括小数 --------------------编程问答-------------------- js验证啊你在这里搜索一下就一大堆 --------------------编程问答-------------------- 在 Text1_Changed 里面判断。
Text1.Text = Val(Text1.Text) --------------------编程问答--------------------
<input type="text" onchange="func(this)"/>--------------------编程问答-------------------- web的话,用js是不错的选择
<script type="text/javascript">
function func(o)
{
if(isNaN(o.value))
{
alert("必须要输入数字!")
o.value=""
return false
}
}
</script>
winform的话,在keyup事件或者textchange事件里判断。 --------------------编程问答-------------------- 正则表达式
Regex. --------------------编程问答-------------------- 为什么不用IsNumeric呢?
--------------------编程问答--------------------
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If Not Microsoft.VisualBasic.IsNumeric(TextBox1.Text) Then
Microsoft.VisualBasic.MsgBox("只能输入数字!")
End If
End Sub
--------------------编程问答-------------------- 1、正則
Regex re = new Regex("^(?!0\\d|[0.]+$)\\d+(\\.\\d+)?$", RegexOptions.None);
2、IsNumeric函數 --------------------编程问答-------------------- 很好的例子,,,可以参考 --------------------编程问答-------------------- 直接百度 js数字验证 --------------------编程问答-------------------- 如果是webform用JS,正则,百度下
如果是WINFORM 把字符串提取出来,整成char[]数组,
然后一个字符一个字符地判断是不是数字 如 isNumber(char) --------------------编程问答-------------------- var reg = /^(?!0\d|[0.]+$)\d+(\.\d*)?$/; --------------------编程问答-------------------- 正则 --------------------编程问答-------------------- js控制比较支持 --------------------编程问答-------------------- 正则 --------------------编程问答--------------------
我觉得这个最简单好用 --------------------编程问答-------------------- Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 47 Then KeyAscii = 0
If KeyAscii < 46 Or KeyAscii > 55 Then KeyAscii = 0
End Sub --------------------编程问答-------------------- 学习了!!! --------------------编程问答-------------------- 如果是winform,使用KeyPress事件判断:
If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Or e.KeyChar = "." Then
e.Handled = False
Else
e.Handled = True
End If --------------------编程问答-------------------- 正则表达式 --------------------编程问答-------------------- 这个方法确实太多了.
用正则表达式,事件,函数都可以 --------------------编程问答-------------------- 用ascii码作判断
--------------------编程问答-------------------- VB Code
Private Sub TextBox1_KeyPress(KeyAscii As Integer)
'控制只允许输入数字
Dim strNumber As String
strNumber = "-0123456789." & Chr(8) 'chr(8)退格键
If InStr(strNumber, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub
--------------------编程问答-------------------- Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> 8 And KeyAscii <> Asc(".") Then
KeyAscii = 0
End If
End Sub
注释退格键的ASCII值是8. --------------------编程问答--------------------
正解!我用过 --------------------编程问答-------------------- 楼上的解答已非常详尽! --------------------编程问答-------------------- 正则表达式比较好 --------------------编程问答-------------------- 自制控件好点,楼上的例子没有排除双小数点和验证负号是否在首位
补充:.NET技术 , VB.NET