日期:2014-05-18  浏览次数:20844 次

TextBox的输入控制
如何控制TextBox只能输入数字而不能输入其它字符??求教求教!!

------解决方案--------------------

C# code

 Regex re = new Regex(@"^\d+$", RegexOptions.None);

            if (!re.IsMatch(TextBox的ID.Text.Trim()))
            {
                 //提示,只能输入数字
            }

------解决方案--------------------
HTML code

<asp:textbox class="Text"
        onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;"
id="txtY_Revenue" runat="server" >
    </asp:textbox>

------解决方案--------------------
WinFom:

 private void txtMaterialQty_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '\b')//这是允许输入退格键
{
if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
{
e.Handled = true;
}
}
}

WebForm:

 <asp:TextBox ID="txtPcbQty" runat="server" Width="159px" AutoCompleteType="Disabled" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"></asp:TextBox>
------解决方案--------------------
C# code

private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)8)
            {
                return;
            }
            if (e.KeyChar > (char)47 && e.KeyChar < (char)58)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }