C#.net中,如何设置textbox只能输入数字?
thanx。能否给各代码? --------------------编程问答-------------------- onkeyup="this.value.replace('',/[^\d]+/g)"
看看好用不 --------------------编程问答-------------------- 这样的问题不要太多啊,你搜一下,一大把 --------------------编程问答-------------------- 就是一个正则的限制吗 --------------------编程问答-------------------- keypress事件里,看情况设置e.Handle
例如
if (e.KeyChar<'0' || e.KeyChar>'9')
e.Handle=true; --------------------编程问答-------------------- <asp:textbox onkeypress="isnum()" id="TextBox1" runat="server"></asp:textbox>
<script language="javascript">
function isnum()
{
if(event.keyCode<45 || event.keyCode>57)
{
event.keyCode=0;
}
}
</script> --------------------编程问答-------------------- if (Regex.IsMatch(str, @"^[0-9]+$"))
{
return true;
}
else
{
return false;
} --------------------编程问答-------------------- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
{
e.Handled = true;
}
} --------------------编程问答-------------------- e.Handled = true; --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- <input
onkeypress="return event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"
onpaste="return !clipboardData.getData('text').match(/\D/)"
ondragenter="return false"
style="ime-mode:Disabled"
>只能输入数字,防止粘贴
--------------------编程问答-------------------- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) )
{
e.Handled = true;
}
}
--------------------编程问答-------------------- 用正则,^[\d]+$ --------------------编程问答-------------------- if (!Regex.IsMatch(this.txtNum.Text, @"^-?\d+(\.\d{2})?$"))
{
MessageBox.Show("User num is invalid");
}
else
{
MessageBox.Show("User num is usable");
} --------------------编程问答-------------------- 以上方法太繁琐了,可以利用数字控件
补充:.NET技术 , C#