Skip to content

Commit 79e86e8

Browse files
Improve numeric input handling for all locales (including Persian and Arabic) (#2240)
* Improve numeric input support in JS: accept all Unicode digits using `\p{Nd}` Replaced hardcoded digit ranges with Unicode-based digit detection using `\p{Nd}‍` to ensure support for Persian, Arabic, and other locale-specific numerals. * fix: support all Unicode digit inputs in RadzenNumeric Replaced digit handling logic with Unicode-aware checks using `char.IsDigit` to support numeric input from any language, including Persian and Arabic. Previously, non-ASCII digits were blocked, which caused issues for users entering Persian or Arabic numerals. This fix makes the component input-agnostic to digit origin.
1 parent 398c17c commit 79e86e8

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Radzen.Blazor/RadzenNumeric.razor.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,9 @@ async Task SetValue(string value)
346346

347347
private string RemoveNonNumericCharacters(object value)
348348
{
349-
string valueStr = value as string;
350-
if (valueStr == null)
351-
{
352-
valueStr = $"{value}";
353-
}
349+
string valueStr = value as string ?? $"{value}";
350+
351+
valueStr = NormalizeDigits(valueStr);
354352

355353
if (!string.IsNullOrEmpty(Format))
356354
{
@@ -372,6 +370,28 @@ private string RemoveNonNumericCharacters(object value)
372370
return new string(valueStr.Where(c => char.IsDigit(c) || char.IsPunctuation(c)).ToArray()).Replace("%", "");
373371
}
374372

373+
private static string NormalizeDigits(string input)
374+
{
375+
if (string.IsNullOrEmpty(input)) return input;
376+
377+
var sb = new System.Text.StringBuilder(input.Length);
378+
foreach (var ch in input)
379+
{
380+
if (char.GetUnicodeCategory(ch) == System.Globalization.UnicodeCategory.DecimalDigitNumber)
381+
{
382+
var numeric = (int)char.GetNumericValue(ch); // 0..9
383+
if (numeric >= 0 && numeric <= 9)
384+
{
385+
sb.Append((char)('0' + numeric));
386+
continue;
387+
}
388+
}
389+
sb.Append(ch);
390+
}
391+
return sb.ToString();
392+
}
393+
394+
375395
/// <summary>
376396
/// Gets or sets the function which returns TValue from string.
377397
/// </summary>

Radzen.Blazor/wwwroot/Radzen.Blazor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,9 @@ window.Radzen = {
980980
return;
981981
}
982982

983-
var ch = String.fromCharCode(e.charCode);
983+
var ch = e.key;
984984

985-
if ((isInteger ? /^[-\d]$/ : /^[-\d,.]$/).test(ch)) {
985+
if (/\p{Nd}/u.test(ch) || ch === '-' || (!isInteger && ch === decimalSeparator)) {
986986
return;
987987
}
988988

0 commit comments

Comments
 (0)