Restriction or Validation in WPF Integer TextBox
18 Nov 20185 minutes to read
This section explains how to validate or restrict the WPF Integer TextBox control value.
Restrict the value within minimum and maximum value
The Value of the WPF Integer TextBox can be restricted within the maximum and minimum limits. Once the value has reached the maximum or minimum value, the value does not exceed the limit. You can change the minimum and maximum limits by using the MinValue and MaxValue properties.
You can choose when to validate the maximum and minimum limits while changing the values by using the MinValidation and MaxValidation properties.
-
OnKeyPress— When MaxValidation or MinValidation is set toOnKeyPress, the value in theWPF Integer TextBoxis validated shortly after pressing a key. As a result, invalid input is not allowed, and the value does not exceed the maximum and minimum limits. -
OnLostFocus- When MaxValidation or MinValidation is set toOnLostFocus, the value in theWPF Integer TextBoxis validated when the control loses keyboard focus. After validation, when the value is greater than theMaxValueor less than theMinValue, the value will be automatically set toMaxValueorMinValue. -
MaxValueOnExceedMaxDigit - When you give input greater than the specified maximum limit,
MaxValueOnExceedMaxDigitdecides whether to retain the old value or reset to the specified maximum limit. For example, ifMaxValueis set to 100 and you are trying to input 200, theValuewill change to 100 whenMaxValueOnExceedMaxDigitistrue, or 20 will be retained ifMaxValueOnExceedMaxDigitisfalse. -
MinValueOnExceedMinDigit - When you give input less than the specified minimum limit,
MinValueOnExceedMinDigitdecides whether to retain the old value or reset to the specified minimum limit. For example, ifMinValueis set to 200 and theValueis 205, and you try to change the value to 20, theValuewill change to 200 whenMinValueOnExceedMinDigitistrue, or the old value 205 will be retained ifMinValueOnExceedMinDigitisfalse.NOTE
MaxValueOnExceedMaxDigitandMinValueOnExceedMinDigitare only effective when the correspondingMaxValidationandMinValidationproperties are set toOnKeyPress.
<syncfusion:IntegerTextBox x:Name="integerTextBox" Width="150" MaxValue="100" MinValue="10"
MinValueOnExceedMinDigit="True" MaxValueOnExceedMaxDigit="True"
MinValidation="OnKeyPress" MaxValidation="OnLostFocus"/>using Syncfusion.Windows.Shared;
IntegerTextBox integerTextBox = new IntegerTextBox();
integerTextBox.Width = 150;
integerTextBox.Height = 25;
integerTextBox.MinValue = 10;
integerTextBox.MaxValue = 100;
integerTextBox.MinValidation = MinValidation.OnKeyPress;
integerTextBox.MaxValidation = MaxValidation.OnLostFocus;
integerTextBox.MinValueOnExceedMinDigit = true;
integerTextBox.MaxValueOnExceedMaxDigit = true;When MinValidation is set to OnKeyPress, you cannot enter a value less than the MinValue. If you try to enter a value less than the MinValue, the MinValue will be set to the Value property because MinValueOnExceedMinDigit is set to true.

MaxValidation is set to OnLostFocus, so the MaxValidation will be performed only on lost focus.

Read-only mode
The WPF Integer TextBox does not allow user input or edits at runtime when the IsReadOnly property is set to true. The user can still select text and display the cursor in the WPF Integer TextBox by setting the IsReadOnlyCaretVisible property to true. The default value of IsReadOnly is false and the default value of IsReadOnlyCaretVisible is false. The Value can still be changed programmatically in read-only mode.
<syncfusion:IntegerTextBox x:Name="integerTextBox" IsReadOnly="True" Value="78" IsReadOnlyCaretVisible="True"/>using Syncfusion.Windows.Shared;
IntegerTextBox integerTextBox = new IntegerTextBox();
integerTextBox.Value = 78;
integerTextBox.IsReadOnly = true;
integerTextBox.IsReadOnlyCaretVisible = true;
Customizing the Behavior for an Invalid Value
You can customize how the WPF Integer TextBox behaves when the entered value is not equal to the ValidationValue property, by using the InvalidValueBehavior property. It can be customized by the following values:
-
DisplayErrorMessage- Shows a MessageBox with the message “String validation failed” after focus is lost from theWPF Integer TextBox. -
None- Validation will not occur. -
ResetValue- Resets the entered value to0after focus is lost.
NOTE
By default the
ValidationValueproperty value isString.Empty. TheValidationValueis a string; the entered value is compared to it as a string.
<syncfusion:IntegerTextBox Width="120" Height="30"
InvalidValueBehavior="DisplayErrorMessage"
ValidationValue="1222"
VerticalAlignment="Center"
HorizontalAlignment="Center" />using Syncfusion.Windows.Shared;
using System.Windows;
IntegerTextBox integerTextBox = new IntegerTextBox()
{
Height = 30,
Width = 120,
InvalidValueBehavior = InvalidValueBehavior.DisplayErrorMessage,
ValidationValue = "1222",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};