Revised
I’m trying to make a Regex Expression that parsed floating point numbers.
- At most 1 negative (-)
- At most 1 decimal (.)
- At most 3 digits to the left of the decimal
- At most 3 digits to the right of the decimal
I’m at the point where I tried the following:
Regex regex = new Regex(@"^(([\-]?|[\.]?|[\-\.]?|[0-9]?)([0-9]{0,3}))");
bool isMatch = regex.IsMatch(text);
However, inputs like "–" still pass
If you have to do regex:
How about ^-{0,1}\d{0,3}(?:.{0,1}(?<=.)\d{0,3})?$
Here it is with some test values
with regards to the onchange event. I don’t know what format you’re using. In MVVM, the binding will keep the box from being changed incorrectly. In windows forms, you’ll, have to change it yourself. I’d say store the value whenever it’s good to a private property. THen, if the change is invalid, return to the previous value.
additionally, the test in c#