0

I'm using the following regular expression to validate double numbers.

var allowedChars = new Regex(@"^[0-9]*(?:\.[0-9]*)?$");

It seems that the above expression still can not filter the following:

001
1....0
1...
..1
1......01

How can I avoid these?

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 7
    Why aren't you using `double.TryParse()`? – stuartd Jul 11 '16 at 21:45
  • 1
    It matches `001`, but does not match other values you provided. See https://regex101.com/r/yV8gR0/1. You actually allow an empty string, integer or floats, or a dot. I'd use `new Regex(@"^\d*\.?\d+$")` – Wiktor Stribiżew Jul 11 '16 at 21:46
  • Because I don't want the user to enter irrelevant data in the first place. I'm using PreviewTextInput event. So it fires at each key press and won't work when I enter `.` – Vahid Jul 11 '16 at 21:47
  • @WiktorStribiżew I tried and I was able to enter `1...` in the text box. It seems it doesn't filter these kinds of patterns. – Vahid Jul 11 '16 at 21:49
  • I guess you are trying to check one char against the final regex. Use the whole texbox value. – Wiktor Stribiżew Jul 11 '16 at 21:49
  • @WiktorStribiżew Sorry, I can't understand. I don't want the user to be able to enter for example 1....., how can I check the whole textbox? with which event? – Vahid Jul 11 '16 at 21:52
  • 1
    Do you need a special event to access the WPF texbox text value? – Wiktor Stribiżew Jul 11 '16 at 21:54
  • 1
    Whether you use regex or `double.TryParse()`, validation is only useful if you are looking at the entire text. A subset of the text could pass validation even while the whole text value does not. It looks like you are using WPF; both WPF and Winforms have robust text validation mechanisms that you should be using, and in either case using `TryParse()` makes the most sense, since that applies the exact same test that will eventually be used to convert the text to a numeric value. In fact, with the binding done right, WPF will do the validation for you automatically. – Peter Duniho Jul 11 '16 at 21:56
  • If you need help with some _specific_ aspect of validation not addressed in the marked duplicate, please post a new question with a good [mcve] that reliably reproduces whatever problem you're trying to solve, and be sure to describe precisely what specific issue you're having trouble with. – Peter Duniho Jul 11 '16 at 21:57
  • @PeterDuniho Thanks Peter, I will look more into it. I'd appreciate it if you could give me a link to the binding method you are suggesting for validation. – Vahid Jul 11 '16 at 21:58
  • 1
    There's no special link. The default binding behavior is to validate the input as the source data type, and to highlight the control with the default adorner (red outline) if the validation fails. You can add your own validation behavior(s) if you like...there's no one single link, you should research how to validate. A couple of SO topics I have found useful include https://stackoverflow.com/questions/19498485/proper-validation-with-mvvm and https://stackoverflow.com/questions/10596452/passing-state-of-wpf-validationrule-to-view-model-in-mvvm – Peter Duniho Jul 11 '16 at 22:04
  • @PeterDuniho Thank you very much Peter. It seems I was going in the wrong direction. – Vahid Jul 11 '16 at 22:06

0 Answers0