Restriction or Validation in WPF DoubleTextBox
18 Nov 20186 minutes to read
This section explains how to validate or restrict the DoubleTextBox control value.
Restrict the value within minimum and maximum value
The Value of the DoubleTextBox 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 maximum and minimum 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— WhenMaxValidationorMinValidationis set toOnKeyPress, the value in theDoubleTextBoxis 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- WhenMaxValidationorMinValidationis set toOnLostFocus, the value in theDoubleTextBoxis validated when the control loses keyboard focus. The control will accept any value while editing, and validation only takes place after focus is lost. After validation, when the value of theDoubleTextBoxis 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. WhenMaxValueOnExceedMaxDigitisfalse, 20 will be retained and the last entered 0 will be ignored.NOTE
MaxValueOnExceedMaxDigitis only effective when theMaxValidationis set toOnKeyPress. -
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. WhenMinValueOnExceedMinDigitisfalse, the old value 205 will be retained.NOTE
MinValueOnExceedMinDigitis only effective when theMinValidationis set toOnKeyPress.
<syncfusion:DoubleTextBox x:Name="doubleTextBox" Width="150" MaxValue="100" MinValue="10"
MinValueOnExceedMinDigit="True" MaxValueOnExceedMaxDigit="True"
MinValidation="OnKeyPress" MaxValidation="OnLostFocus"/>using Syncfusion.Windows.Shared;
DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Width = 150;
doubleTextBox.Height = 25;
doubleTextBox.MinValue = 10;
doubleTextBox.MaxValue = 100;
doubleTextBox.MinValidation = MinValidation.OnKeyPress;
doubleTextBox.MaxValidation = MaxValidation.OnLostFocus;
doubleTextBox.MinValueOnExceedMinDigit = true;
doubleTextBox.MaxValueOnExceedMaxDigit = true;MinValidation is set to OnKeyPress, it cannot let to enter a value less than the MinValue. If try to enter a value less than the MinValue, then the MinValue will set to the Value property because MinValueOnExceedMinDigit is set to true.

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

Restrict number of decimal digits
You can format the decimal digits in the DoubleTextBox control using the NumberDecimalDigits property. You can restrict the decimal digits of the text within maximum and minimum limits using the MinimumNumberDecimalDigits and MaximumNumberDecimalDigits properties. The default value of MinimumNumberDecimalDigits and MaximumNumberDecimalDigits is -1, which means the value is unbounded.
NOTE
If the value of
MinimumNumberDecimalDigitsis greater than the value ofMaximumNumberDecimalDigits, the text ofDoubleTextBoxwill be updated based on the value ofMinimumNumberDecimalDigits.
<syncfusion:DoubleTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumNumberDecimalDigits="4"
MinimumNumberDecimalDigits="1" />using Syncfusion.Windows.Shared;
DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Value = 125.32545;
doubleTextBox.MaximumNumberDecimalDigits = 4;
doubleTextBox.MinimumNumberDecimalDigits = 1;
When value of MinimumNumberDecimalDigits, MaximumNumberDecimalDigits and NumberDecimalDigits properties are specified, NumberDecimalDigits property takes high precedence and updates the text of DoubleTextBox property.
<syncfusion:DoubleTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumNumberDecimalDigits="4"
MinimumNumberDecimalDigits="1"
NumberDecimalDigits="3"
/>using Syncfusion.Windows.Shared;
DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Value = 125.32545;
doubleTextBox.MaximumNumberDecimalDigits = 4;
doubleTextBox.MinimumNumberDecimalDigits = 1;
doubleTextBox.NumberDecimalDigits = 3;
Read-only mode
The DoubleTextBox does not allow user input or edits when the IsReadOnly property is set to true. The user can still select text and display the cursor in the DoubleTextBox 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:DoubleTextBox x:Name="doubleTextBox" IsReadOnly="True" Value="10" IsReadOnlyCaretVisible="True"/>DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Value = 10;
doubleTextBox.IsReadOnly = true;
doubleTextBox.IsReadOnlyCaretVisible = true;