小编Joh*_*ith的帖子

在C#中使用RegEx验证浮点数

我试图只TextBox在WPF中创建一个数字,我有这个代码:

void NumericTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    e.Handled = !IsValidInput(e.Text);
}

private bool IsValidInput(string p)
{
    switch (this.Type)
    {
        case NumericTextBoxType.Float:
            return Regex.Match(p, "^[0-9]*[.][0-9]*$").Success;
        case NumericTextBoxType.Integer:                    
        default:
            return Regex.Match(p, "^[0-9]*$").Success;                    
    }
}

// And also this!
public enum NumericTextBoxType
{
    Integer = 0, 
    Float = 1
}
Run Code Online (Sandbox Code Playgroud)

当我将类型设置为Integer时,它运行良好,但对于Float,它没有.

我可以使用那么多NumericTextBox控件,但我想知道为什么这个不起作用?

c# regex validation wpf

10
推荐指数
2
解决办法
3万
查看次数

标签 统计

c# ×1

regex ×1

validation ×1

wpf ×1