I have a requirement to make a decimal textbox (Money textbox) which:
- only allows numbers 0-9 (allow upper numpad keys 0-9 and right numpad keys 0-9);
- allows only one dot which don't appear on start.
Valid:
- 0.5
- 1
- 1.5000
Invalid:
- .5
- 5.500.55
Edit
my code is :
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;
}
}