在TextChange事件里引用该方法。 public static void ValidNum(System.Windows.Forms.TextBox textbox, int minValue, int maxValue) { if (textbox.Text=="") return; if (IsNumeric(textbox.Text)==false||Convert.ToDouble(textbox.Text)<minValue||Convert.ToDouble(textbox.Text)>maxValue) { textbox.Text=textbox.Text.Substring(0,textbox.Text.Length-1); if (textbox.Text.Length>=1) { textbox.SelectionStart=textbox.Text.Length; } }
ID="RegularExpressionValidator1" runat="server" ErrorMessage="*" ControlToValidate="TextBox1" ValidationExpression="\d*"></asp:RegularExpressionValidator>
还是
ASP.NET
用MaskTextBox,设置Mask好了
public static void ValidNum(System.Windows.Forms.TextBox textbox, int minValue, int maxValue)
{
if (textbox.Text=="")
return; if (IsNumeric(textbox.Text)==false||Convert.ToDouble(textbox.Text)<minValue||Convert.ToDouble(textbox.Text)>maxValue)
{
textbox.Text=textbox.Text.Substring(0,textbox.Text.Length-1);
if (textbox.Text.Length>=1)
{
textbox.SelectionStart=textbox.Text.Length;
}
}
}
中策:声明一个int(或根据需要,double)属性,将TextBox的Text绑定上去
下策:自写代码实现
就用keypress事件就可以了
private void txtJan_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e)
{
if( !( e.KeyChar <= '9' && e.KeyChar >= '0' ))
{
e.Handled=true;
}
}