In Blazor Server net8,
I am trying to limit the number of decimal digits the user can input in <MudNumericField>
For example if I want to limit it to two(2) decimal digits, then the input field should ignore the next decimal input.
Though I still want to preserve the comma formatting for the whole number for the thousands place. i.e. 1,000.00
I have tried using Value and ValueChanged but it still allows the user to input, the excess decimal digits are only removed(truncated) if the numericField loses focus but it rounds off the value like 99.999 will become 100 when I want it to be 99.99, so I think preventing user input will be better
<MudNumericField @bind-Value="Allowance" T="decimal" Culture="@(CultureInfo.GetCultureInfo("en-US"))" Format="C2" Min="0" Immediate" />private decimal Allowance { get; set; } = 0.0m;I also tried OnKeyPress, OnKeyDown, and OnKeyUp, but still does not behave the way I expect it since there is a split second where it appends the input, then quickly removes it, but I may be doing it wrong
private void OnKeyUpEvent(KeyboardEventArgs e){ string allowanceString = Allowance.ToString("G29", CultureInfo.InvariantCulture); int decimalIndex = allowanceString.IndexOf('.'); if (decimalIndex >= 0 && allowanceString.Length - decimalIndex - 1 > 10) { allowanceString = allowanceString.Substring(0, decimalIndex + 11); if (decimal.TryParse(allowanceString, out var truncatedAllowance)) { Allowance = truncatedAllowance; } }}