C# 验证文本框以仅允许数值

C# 验证文本框以仅允许数值,c#,validation,C#,Validation,可能重复: 我有一个电话号码,我希望存储为字符串 我是用英语读的 txtHomePhone.Text 我想我需要的是某种数字,但不能让它工作 if (txtHomePhone.Text == //something.IsNumeric) { //Display error } else { //Carry on with the rest of program ie. Add Phone number to program. } 只允许输入数值的最佳方式是什么 试试这个 i

可能重复:

我有一个电话号码,我希望存储为字符串

我是用英语读的

txtHomePhone.Text
我想我需要的是某种数字,但不能让它工作

if (txtHomePhone.Text == //something.IsNumeric)
{
    //Display error
}
else
{
    //Carry on with the rest of program ie. Add Phone number to program.
}
只允许输入数值的最佳方式是什么

试试这个

if (!txtHomePhone.Text.All(c=> Char.IsNumber(c)))
{
    //Display error
}
else
{
    //Carry on with the rest of program ie. Add Phone number to program.
}

要检查是否输入了数值,可以使用
Integer.TryParse

int num;
bool isNum = Integer.TryParse(txtHomePhone.Text.Trim(), out num);

if (!isNum)
    //Display error
else
    //Carry on with the rest of program ie. Add Phone number to program.

但请记住,电话号码不一定只是数字。请参阅Trevor Pilley answer以获取蒙面文本框。

我假设您在这里使用的是Windows窗体,请查看。它允许您指定字符的输入掩码

txtHomePhone.Mask = "##### ### ###";
由于这允许您限制输入值,因此可以安全地将值解析为整数


注意:如果您使用的是WPF,我认为基本库中没有MaskedTextBox,但是有一些扩展可以提供类似的功能。

通常我会按照davenewza的建议使用Integer.TryParse,但另一种选择是使用C#中的VisualBasic IsNumeric函数

添加对Microsoft.VisualBasic.dll文件的引用,然后使用以下代码

if (Microsoft.VisualBasic.Information.IsNumeric(txtHomePhone.Text))
{
    //Display error
}
else
{
    //Carry on with the rest of program ie. Add Phone number to program.
}

由于
txtHomePhone
表示一个
文本框
,因此您可以使用
按键
事件来接受您希望允许的字符,并拒绝您不希望在
txtHomePhone中允许的字符

示例

public Form1()
{
    InitializeComponent();
    txtHomePhone.KeyPress += new KeyPressEventHandler(txtHomePhone_KeyPress);
}
private void txtHomePhone_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The  character represents a backspace
    {
        e.Handled = false; //Do not reject the input
    }
    else
    {
        e.Handled = true; //Reject the input
    }
}
if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The  character represents a backspace
{
    e.Handled = false; //Do not reject the input
}
else
{
    if (e.KeyChar == ')' && !txtHomePhone.Text.Contains(")"))
    {
        e.Handled = false; //Do not reject the input
    }
    else if (e.KeyChar == '(' && !txtHomePhone.Text.Contains("("))
    {
        e.Handled = false; //Do not reject the input
    }
    else if (e.KeyChar == '-' && !textBox1.Text.Contains("-"))
    {
        e.Handled = false; //Do not reject the input
    }
    else if (e.KeyChar == ' ' && !txtHomePhone.Text.Contains(" "))
    {
        e.Handled = false; //Do not reject the input
    }
    else
    {
        e.Handled = true;
    }
}
谢谢,

我希望这对您有所帮助:)

我发现最好的方法是只允许用户在文本框中输入数字键,谢谢您的帮助

txtHomePhone
是否表示
文本框
?如果是,您可以使用
按键
事件接受您希望允许的字符,并拒绝您不希望在
文本框
中输入的字符。祝你度过愉快的一天:)它确实代表一个文本框,我如何找出哪个按键是哪个?这是一个web应用程序还是客户端应用程序?Springfox,看看@Tyrionlanister提供的链接-第一个答案有一些代码,告诉你如何处理按键事件,并且只允许数字通过。如果您希望保留上面的If功能,请查看下面davenewza的答案(TryParse)。例如,您还可以使用正则表达式来匹配更复杂的字符串,并检测字符串是否使用了“(xxx)xxx-xxxx”格式?它给出了一个真/假值,但没有给出任何真正的验证。(同样,对于原始问题中使用的电话号码示例,这可能不够:对于典型电话号码输入中可能存在的符号和空格,它将返回false。)@DanPuzey在他的示例中特别写道:if(txtHomePhone.Text==///something.IsNumeric)。所以我认为没有whitspaces等。关于验证,他所要做的就是检查isNumeric。我将代码添加到if语句中以更好地解释。他正在寻找一种方法来验证数字是否为数字,而不是隐藏数字。掩码指定允许的字符,它不会隐藏输入,就像密码输入掩码一样。@TyrionLannister a Maskedtextbox不会隐藏它强制输入符合密码的值“面具”。在特雷弗的例子中,文本框只会显示11个格式为
“###################”
的数字。我的道歉。时间还早,我读到了“面具”。@Tyrionlanister-很容易,我们都到了:o)我很高兴你的问题得到了解决。。祝你有一个愉快的一天:)(例如,我在visual basic.netSuper中尝试了这个有用的答案,谢谢。我必须在visual C#MVStudio环境中使用e.KeyChar='\b'来捕获退格(而不是e.KeyChar='')。我使用您的模式来接受特定字符的一个实例,接受数值中的小数点。