C#Winform验证输入框的内容是数字

判断字符串是否数字:
C# Code:
/// <summary>
/// 验证数字
/// </summary>
/// <param name="number">数字内容</param>
/// <returns>true 验证成功 false 验证失败</returns>
public bool CheckNumber(string number)
{
if (string.IsNullOrEmpty(number))
{
return false;
}
Regex regex = new Regex(@"^(-)?\d+(\.\d+)?$");
if (regex.IsMatch(number))
{
return true;
}
else
{
return false;
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// <summary>
/// 验证数字
/// </summary>
/// <param name="number">数字内容</param>
/// <returns>true 验证成功 false 验证失败</returns>
public bool CheckNumber(string number)
{
if (string.IsNullOrEmpty(number))
{
return false;
}
Regex regex = new Regex(@"^(-)?\d+(\.\d+)?$");
if (regex.IsMatch(number))
{
return true;
}
else
{
return false;
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
输入框仅允许输入数字:
private void txtUserId_KeyPress(object sender, KeyPressEventArgs e)
{
//如果输入的不是数字键,也不是回车键、Backspace键,则取消该输入if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar!=(char)13 && e.KeyChar!=(char)8){
e.Handled = true;
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网