Restriction or Validation in WPF Currency TextBox
6 Jul 20216 minutes to read
This section explains how to validate or restrict the CurrencyTextBox
control value.
Restrict the value within minimum and maximum value
The Value of the CurrencyTextBox 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. We can change the maximum and minimum limits by using the MinValue property and MaxValue property.
You can choose when to validate the maximum and minimum limits while changing the values by using the MinValidation and MaxValidation properties.
-
OnKeyPress
— When setting theMaxValidation
orMinValidation
toOnKeyPress
, the value in theCurrencyTextBox
will be validated shortly after pressing a key. So, it is not possible to provide any invalid input at all and the value does not exceed the maximum and minimum limits. -
OnLostFocus
- When settingMaxValidation
orMinValidation
toOnLostFocus
, the value in theCurrencyTextBox
is validated, when theCurrencyTextBox
loses the focus. That is, theCurrencyTextBox
will accept any value, validation will only take place after theCurrencyTextBox
has lost its keyboard focus. After validation, when the value of theCurrencyTextBox
is greater than theMaxValue
or less than theMinValue
, the value will be automatically set toMaxValue
orMinValue
. -
MaxValueOnExceedMaxDigit - When you give input greater than specified maximum limit,
MaxValueOnExceedMaxDigit
property will decide either it should retain the old value or reset to maximum limit that is specified. For example, ifMaxValue
is set to 100 and you are trying to input 200.Value
will changed to 100 whenMaxValueOnExceedMaxDigit
istrue
. WhenMaxValueOnExceedMaxDigit
isfalse
, 20 will be retained and last entered 0 will be ignored.NOTE
MaxValueOnExceedMinDigit
property will be enabled only when theMaxValidation
is set toOnKeyPress
. -
MinValueOnExceedMinDigit - When you give input less than specified minimum limit,
MinValueOnExceedMinDigit
property will decide either it should retain the old value or reset to minimum limit that is specified. For example, ifMinValue
is set to 200 and theValue
is 205 and you are trying change the value to 20.Value
will changed to 200 whenMinValueOnExceedMinDigit
istrue
. WhenMinValueOnExceedMinDigit
isfalse
, Old value 205 will be retained.NOTE
MinValueOnExceedMinDigit
will be enabled only when theMinValidation
is set toOnKeyPress
.
<syncfusion:CurrencyTextBox x:Name="currencyTextBox" Width="150" MaxValue="100" MinValue="10"
MinValueOnExceedMinDigit="True" MaxValueOnExceedMaxDigit="True"
MinValidation="OnKeyPress" MaxValidation="OnLostFocus"/>
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 150;
currencyTextBox.Height = 25;
currencyTextBox.MinValue = 10;
currencyTextBox.MaxValue =100;
currencyTextBox.MinValidation = MinValidation.OnKeyPress;
currencyTextBox.MaxValidation = MaxValidation.OnLostFocus;
currencyTextBox.MinValueOnExceedMinDigit = true;
currencyTextBox.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 CurrencyTextBox control using CurrencyDecimalDigits property. You can also restrict the decimal digits of the text within minimum and maximum limit in CurrencyTextBox
control using MinimumCurrencyDecimalDigits and MaximumCurrencyDecimalDigits properties. The default value of MinimumCurrencyDecimalDigits
,MaximumCurrencyDecimalDigits
and CurrencyDecimalDigits
properties is -1.
NOTE
If the value of
MinimumCurrencyDecimalDigits
property is greater than the value ofMaximumCurrencyDecimalDigits
property, the text ofCurrencyTextBox
will be updated based on value ofMinimumCurrencyDecimalDigits
property.
<syncfusion:CurrencyTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumCurrencyDecimalDigits="4"
MinimumCurrencyDecimalDigits="1" />
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Value = 125.32545;
currencyTextBox.MaximumCurrencyDecimalDigits = 4;
currencyTextBox.MinimumCurrencyDecimalDigits = 1;
When the value of MinimumCurrencyDecimalDigits
, MaximumCurrencyDecimalDigits
and CurrencyDecimalDigits
properties are specified, CurrencyDecimalDigits
property takes high precedence and updates the text of CurrencyTextBox
property.
<syncfusion:CurrencyTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumCurrencyDecimalDigits="4"
MinimumCurrencyDecimalDigits="1"
CurrencyDecimalDigits="3"
/>
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Value = 125.32545;
currencyTextBox.MaximumCurrencyDecimalDigits = 4;
currencyTextBox.MinimumCurrencyDecimalDigits = 1;
currencyTextBox.CurrencyDecimalDigits = 3;
Read only mode
The CurrencyTextBox
cannot allow the user input, edits when IsReadOnly property is sets to true
. The user can still select text and display the cursor on the CurrencyTextBox
by setting the IsReadOnlyCaretVisible property to true
. However, value can be changed programmatically in readonly mode.
<syncfusion:CurrencyTextBox x:Name="currencyTextBox" IsReadOnly="True" Value="10" IsReadOnlyCaretVisible="True"/>
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Value = 10;
currencyTextBox.IsReadOnly = true;
currencyTextBox.IsReadOnlyCaretVisible = true;