日期:2014-05-19  浏览次数:20794 次

如何让一个TextBox只接受0-10这10个数字
如题

------解决方案--------------------
onkeyup= "value= "/value.replace(/[ "^0-9|10]/g, ' ') "onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^0-9|10]/g, ' ')) "
------解决方案--------------------
问题应该是0-9这10个数字吧?
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar > = (char)48 && e.KeyChar <= (char)57)
{
e.Handled = false;
}
else
{
e.Handled = true;
}

}
------解决方案--------------------
using System.Runtime.InteropServices;

[DllImport( "User32.dll ")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport( "User32.dll ")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
public const int GWL_STYLE = -16;
public const int ES_NUMBER = 0x2000;
private void Form1_Load(object sender, EventArgs e)
{
SetWindowLong(textBox1.Handle, GWL_STYLE,
GetWindowLong(textBox1.Handle, GWL_STYLE) | ES_NUMBER);
}

// or

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = "0123456789 ".IndexOf(e.KeyChar) < 0;
}

------解决方案--------------------
判断是否只含数字
private bool isNum(string keyChar)
{
int i = 0;
char c;
for (i = 0; i < keyChar.Length; i++)
{
c = keyChar[i];
if (!(c > = 48 && c <= 57))
{
return false;
}
}
return true;
}
------解决方案--------------------
如果是服务器的TEXTBOX控件
设置maxlenth属性,你的情况是填写10
------解决方案--------------------
然后写个方法判断char是否为数字,使用system.convert.toint32()方法。如果字符不是数字,返回的好象是0或-1。
在KeyDown事件带入方法
------解决方案--------------------
<input onkeyup= "value=value.replace(/[^\d]/g, ' ') "onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\d]/g, ' ')) ">


------解决方案--------------------
<input style= "ime-mode:disabled " onkeydown= "if(event.keyCode==13)event.keyCode=9 " onKeyPress= "if ((event.keyCode <48 || event.keyCode> 57)) event.returnValue=false ">
------解决方案--------------------
\d+
------解决方案--------------------
只能输入数字:onkeyup= "value=value.replace(/[^\d]/g, ' ') "onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\d]/g, ' ')) "