Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF: How to make TextBox only allow enter certain range of numbers?

$
0
0

In our WPF application, we need to restrict a TextBox to only allow to enter number from 5 to 9999.

In WPF, we could implement the following to only allow the number input

in XAML, define TextBox's PreviewTextInput = "NumericOnly"

private void NumericOnly(object sender, TextCompositionEventArgs e) { e.Handled = Utility.IsTextNumeric(e.Text); }

public static bool IsTextNumeric(string str)
{
      Regex reg = new Regex("[^0-9]");
      return reg.IsMatch(str);
}
To only 9999, we can set TextBox MaxLength = "4".

We know we could use validation input value to achieve the goal.

Is there a easy way to only allow number from 5 to 9999 by TextBox? Thx!


JaneC



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>