【发布时间】:2013-12-19 12:51:01
【问题描述】:
我需要制作一个十进制文本框(Money 文本框):
- 仅允许数字 0-9(允许上小键盘键 0-9 和右小键盘 键 0-9);
- 只允许一个不出现在开头的点。
有效:
- 0.5
- 1
- 1.5000
无效:
- .5
- 5.500.55
编辑
我的代码是:
private void floatTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !TextBoxValidation.AreAllValidDecimalChars(e.Text);
}
public static class TextBoxValidation
{
public static bool AreAllValidDecimalChars(string str)
{
bool ret = false;
int x = 0;
foreach (char c in str)
{
x = (int)c;
}
if (x >= 48 && x <= 57 || x == 46)
{
ret = true;
}
return ret;
}
}
【问题讨论】:
-
好的,所以你有一个要求。请向我们展示你的努力。并解释为什么你的努力失败了。
-
这里有一个类似的问题,答案是stackoverflow.com/questions/16914224/…
-
@PhoenixReborn,请看我的编辑
-
“向我们解释为什么你的努力失败了”....
-
@Mussammil 您在循环之外进行了验证。这只会检查最后一个字符。此外,如果它是正确的,如果 ANY 字符是 ok 而不是 ALL,它将返回 true。
标签: c# .net wpf validation textbox