fix(AmountInput): Skip non-digits for max input length limit validation

Closes: #9718
This commit is contained in:
Michał Cieślak 2023-03-02 12:33:14 +01:00 committed by Michał
parent 405a8ee3ed
commit 0e60ac4933
2 changed files with 27 additions and 1 deletions

View File

@ -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 {

View File

@ -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
}