-1

I sank most of last night looking into this and found lots of half answers that didn't quite work for my fairly common issue. I have a Textbox that i want to save to an Integer in a Database. I need to only allow the user to enter valid integers with no spaces so that:

-14   valid
14    valid
0     valid
      invalid
14.3  invalid
1-4   invalid
A14   invalid
14A   invalid
"14   " (has whitespace)  invalid 
  14   (has whitespace) invalid
1    4  (has whitespace)  invalid

you get the picture. only integers.

form what iv'e found this method is the key. but I cant figure out/find the regex/alternative to validate it correctly

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        //Validation code here
    }
bodovix
  • 299
  • 2
  • 4
  • 11
  • 1
    Possible duplicate of [How do I get a TextBox to only accept numeric input in WPF?](https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf) – ASh Oct 31 '18 at 09:26
  • I did find that one, the accepted anser was pants. a lot of them work for negatives, but would let 1-4 through. wich would be invalid. but i'll go a bit deeper into it too see it brings anything else to the table – bodovix Oct 31 '18 at 09:29
  • 1
    if highly upvoted answer doesn't help you solve some issue, then be *very specific* what exactly doesn't work and create a [MCVE] – ASh Oct 31 '18 at 09:31
  • the problem was the regexes in the sugested answer didn't validate the position of the - in negative numbers, or prevent white space. wich would break my Database calls. however the answer to my problem was way down on the list of answers to that question so thanks for the push to go deeper, – bodovix Oct 31 '18 at 09:54

3 Answers3

1

There's no need to reinvent the wheel.

Use a control such as IntegerUpDown from the Extended WPF Toolkit

Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • Why the downvote? If the OP wants to see how that control works, the source code is available for download. – Peregrine Oct 31 '18 at 09:48
  • I don't think the extended WPF toolkit is going anywhere; the problem I have is that it's an already built solution, that could be incompatible with what OP wants, or simply add too much boilerplate code. The source code is here alright... Might as well quote it then! – Kilazur Oct 31 '18 at 13:49
0

I've been using MVVM so after working through a bunch of the answers in the sugested "Sugested Answer" the Approved answer didn't validate it correctly. and all the Regex examples failed to account for the order of the - when dealing with negatives.

But using MVVM you can make the binding "Mode=TwoWay and UpdateSourceTrigger=PropertyChanged" then it validates it's self.

wont work if you don't want two way binding, so the ideal regex solution is still to be solved.

but this solves my issue.

bodovix
  • 299
  • 2
  • 4
  • 11
0

I use regex for this : private static readonly Regex _regex = new Regex("[^0-9]");

you can bool property for it private static bool IsTextAllowed(string text) { return !_regex.IsMatch(text); }

and then call it private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = !IsTextAllowed(e.Text); }