From 0e60ac4933f2fd9270af839faba604d266eefa30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Thu, 2 Mar 2023 12:33:14 +0100 Subject: [PATCH] fix(AmountInput): Skip non-digits for max input length limit validation Closes: #9718 --- ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml | 21 +++++++++++++++++++++ ui/imports/shared/controls/AmountInput.qml | 7 ++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml b/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml index af91f4f5eb..6e141d14de 100644 --- a/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml +++ b/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml @@ -57,6 +57,18 @@ QtObject { } } + function getLocalizedDigitsCount(str, locale = null) { + if (!str) + return 0 + + locale = locale || Qt.locale() + + if (d.nonDigitCharacterRegExpLocale !== locale) + d.nonDigitCharacterRegExpLocale = locale + + return str.replace(d.nonDigitCharacterRegExp, "").length + } + function currencyAmountToLocaleString(currencyAmount, options = null, locale = null) { locale = locale || Qt.locale() @@ -146,6 +158,15 @@ QtObject { secondDate.setHours(0, 0, 0) return Math.round(Math.abs((firstDate - secondDate) / d.msInADay)) // Math.round: not all days are 24 hours long! } + + property var nonDigitCharacterRegExpLocale + + readonly property var nonDigitCharacterRegExp: { + const localizedNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map( + n => LocaleUtils.numberToLocaleString(n, 0, nonDigitCharacterRegExpLocale)) + + return new RegExp(`[^${localizedNumbers.join("")}]`, "g") + } } readonly property Settings settings: Settings { diff --git a/ui/imports/shared/controls/AmountInput.qml b/ui/imports/shared/controls/AmountInput.qml index a3a902ea4d..8d35154ad5 100644 --- a/ui/imports/shared/controls/AmountInput.qml +++ b/ui/imports/shared/controls/AmountInput.qml @@ -32,6 +32,11 @@ Input { id: d property real amount: 0 + + function getEffectiveDigitsCount(str) { + const digits = LocaleUtils.getLocalizedDigitsCount(text, root.locale) + return str.startsWith(locale.decimalPoint) ? digits + 1 : digits + } } validator: DoubleValidator { @@ -53,7 +58,7 @@ Input { return } - if (text.length > root.maximumLength) { + if (d.getEffectiveDigitsCount(text) > root.maximumLength) { root.validationError = qsTr("The maximum number of characters is %1").arg(root.maximumLength) return }