| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickvalidator_p.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | #if QT_CONFIG(validator) |
| 9 | |
| 10 | /*! |
| 11 | \qmltype IntValidator |
| 12 | \nativetype QIntValidator |
| 13 | \inqmlmodule QtQuick |
| 14 | \ingroup qtquick-text-utility |
| 15 | \ingroup qtquick-text-validators |
| 16 | \brief Defines a validator for integer values. |
| 17 | |
| 18 | The IntValidator type provides a validator for integer values. |
| 19 | |
| 20 | If no \l locale is set IntValidator uses the \l {QLocale::setDefault()}{default locale} to |
| 21 | interpret the number and will accept locale specific digits, group separators, and positive |
| 22 | and negative signs. In addition, IntValidator is always guaranteed to accept a number |
| 23 | formatted according to the "C" locale. |
| 24 | |
| 25 | The following example shows a TextInput object with an IntValidator to check |
| 26 | that the user has input an integer within the specified range, updating the |
| 27 | text color to highlight invalid input: |
| 28 | |
| 29 | \snippet qml/intvalidator.qml 0 |
| 30 | |
| 31 | \sa DoubleValidator, RegularExpressionValidator, {Validating Input Text} |
| 32 | */ |
| 33 | |
| 34 | QQuickIntValidator::QQuickIntValidator(QObject *parent) |
| 35 | : QIntValidator(parent) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /*! |
| 40 | \qmlproperty string QtQuick::IntValidator::locale |
| 41 | |
| 42 | This property holds the name of the locale used to interpret the number. |
| 43 | |
| 44 | \sa {QtQml::Qt::locale()}{Qt.locale()} |
| 45 | */ |
| 46 | |
| 47 | QString QQuickIntValidator::localeName() const |
| 48 | { |
| 49 | return locale().name(); |
| 50 | } |
| 51 | |
| 52 | void QQuickIntValidator::setLocaleName(const QString &name) |
| 53 | { |
| 54 | if (locale().name() != name) { |
| 55 | setLocale(QLocale(name)); |
| 56 | emit localeNameChanged(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void QQuickIntValidator::resetLocaleName() |
| 61 | { |
| 62 | QLocale defaultLocale; |
| 63 | if (locale() != defaultLocale) { |
| 64 | setLocale(defaultLocale); |
| 65 | emit localeNameChanged(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /*! |
| 70 | \qmlproperty int QtQuick::IntValidator::top |
| 71 | |
| 72 | This property holds the validator's highest acceptable value. |
| 73 | By default, this property's value is derived from the highest signed integer available (typically 2147483647). |
| 74 | */ |
| 75 | /*! |
| 76 | \qmlproperty int QtQuick::IntValidator::bottom |
| 77 | |
| 78 | This property holds the validator's lowest acceptable value. |
| 79 | By default, this property's value is derived from the lowest signed integer available (typically -2147483647). |
| 80 | */ |
| 81 | |
| 82 | /*! |
| 83 | \qmltype DoubleValidator |
| 84 | \nativetype QDoubleValidator |
| 85 | \inqmlmodule QtQuick |
| 86 | \ingroup qtquick-text-utility |
| 87 | \ingroup qtquick-text-validators |
| 88 | \brief Defines a validator for non-integer numbers. |
| 89 | |
| 90 | The DoubleValidator type provides a validator for non-integer numbers. |
| 91 | |
| 92 | \list |
| 93 | \li Accepted Input: Input is accepted if it contains a double that is within |
| 94 | the valid range and is in the correct format. |
| 95 | \li Accepted but Invalid Input: Input is accepted but considered invalid if |
| 96 | it contains a double that is outside the valid range or is in the wrong |
| 97 | format (for example, too many digits after the decimal point or empty). |
| 98 | \li Rejected Input: Input is rejected if it is not a double. |
| 99 | \endlist |
| 100 | |
| 101 | \note If the valid range consists of only positive doubles (for example, |
| 102 | 0.0 to 100.0) and the input is a negative double, it is rejected. If |
| 103 | \l notation is set to \c DoubleValidator.StandardNotation and the input |
| 104 | contains more digits before the decimal point than a double in the valid |
| 105 | range may have, it is also rejected. If \l notation is |
| 106 | \c DoubleValidator.ScientificNotation and the input is not in the valid |
| 107 | range, it is accepted but invalid. The value may become valid by changing |
| 108 | the exponent. |
| 109 | |
| 110 | The following example shows a TextInput object with a DoubleValidator to |
| 111 | check that the user has input a double within the specified range, |
| 112 | updating the text color to highlight invalid input: |
| 113 | |
| 114 | \snippet qml/doublevalidator.qml 0 |
| 115 | |
| 116 | \sa IntValidator, RegularExpressionValidator, {Validating Input Text} |
| 117 | */ |
| 118 | |
| 119 | QQuickDoubleValidator::QQuickDoubleValidator(QObject *parent) |
| 120 | : QDoubleValidator(parent) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | \qmlproperty string QtQuick::DoubleValidator::locale |
| 126 | |
| 127 | This property holds the name of the locale used to interpret the number. |
| 128 | |
| 129 | \sa {QtQml::Qt::locale()}{Qt.locale()} |
| 130 | */ |
| 131 | |
| 132 | QString QQuickDoubleValidator::localeName() const |
| 133 | { |
| 134 | return locale().name(); |
| 135 | } |
| 136 | |
| 137 | void QQuickDoubleValidator::setLocaleName(const QString &name) |
| 138 | { |
| 139 | if (locale().name() != name) { |
| 140 | setLocale(QLocale(name)); |
| 141 | emit localeNameChanged(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void QQuickDoubleValidator::resetLocaleName() |
| 146 | { |
| 147 | QLocale defaultLocale; |
| 148 | if (locale() != defaultLocale) { |
| 149 | setLocale(defaultLocale); |
| 150 | emit localeNameChanged(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /*! |
| 155 | \qmlproperty real QtQuick::DoubleValidator::top |
| 156 | |
| 157 | This property holds the validator's maximum acceptable value. |
| 158 | By default, this property contains a value of infinity. |
| 159 | */ |
| 160 | /*! |
| 161 | \qmlproperty real QtQuick::DoubleValidator::bottom |
| 162 | |
| 163 | This property holds the validator's minimum acceptable value. |
| 164 | By default, this property contains a value of -infinity. |
| 165 | */ |
| 166 | /*! |
| 167 | \qmlproperty int QtQuick::DoubleValidator::decimals |
| 168 | |
| 169 | This property holds the validator's maximum number of digits after the decimal point. |
| 170 | By default, this property contains a value of 1000. |
| 171 | */ |
| 172 | /*! |
| 173 | \qmlproperty enumeration QtQuick::DoubleValidator::notation |
| 174 | This property holds the notation of how a string can describe a number. |
| 175 | |
| 176 | The possible values for this property are: |
| 177 | |
| 178 | \value DoubleValidator.StandardNotation only decimal numbers with optional sign (e.g. \c -0.015) |
| 179 | \value DoubleValidator.ScientificNotation (default) the written number may have an exponent part (e.g. \c 1.5E-2) |
| 180 | */ |
| 181 | |
| 182 | /*! |
| 183 | \qmltype RegularExpressionValidator |
| 184 | \nativetype QRegularExpressionValidator |
| 185 | \inqmlmodule QtQuick |
| 186 | \ingroup qtquick-text-utility |
| 187 | \ingroup qtquick-text-validators |
| 188 | \brief Provides a string validator. |
| 189 | \since 5.14 |
| 190 | |
| 191 | The RegularExpressionValidator type provides a validator, that counts as valid any string which |
| 192 | matches a specified regular expression. |
| 193 | |
| 194 | \sa IntValidator, DoubleValidator, {Validating Input Text} |
| 195 | */ |
| 196 | /*! |
| 197 | \qmlproperty regularExpression QtQuick::RegularExpressionValidator::regularExpression |
| 198 | |
| 199 | This property holds the regular expression used for validation. |
| 200 | |
| 201 | Note that this property should be a regular expression in JS syntax, e.g /a/ for the regular |
| 202 | expression matching "a". |
| 203 | |
| 204 | By default, this property contains a regular expression with the pattern \c{.*} that matches any |
| 205 | string. |
| 206 | |
| 207 | Below you can find an example of a \l TextInput object with a RegularExpressionValidator |
| 208 | specified: |
| 209 | |
| 210 | \snippet qml/regularexpression.qml 0 |
| 211 | |
| 212 | Some more examples of regular expressions: |
| 213 | |
| 214 | \list |
| 215 | \li A list of numbers with one to three positions separated by a comma: |
| 216 | \badcode |
| 217 | /\d{1,3}(?:,\d{1,3})+$/ |
| 218 | \endcode |
| 219 | |
| 220 | \li An amount consisting of up to 3 numbers before the decimal point, and |
| 221 | 1 to 2 after the decimal point: |
| 222 | \badcode |
| 223 | /(\d{1,3})([.,]\d{1,2})?$/ |
| 224 | \endcode |
| 225 | \endlist |
| 226 | */ |
| 227 | |
| 228 | #endif // validator |
| 229 | |
| 230 | QT_END_NAMESPACE |
| 231 | |
| 232 | #include "moc_qquickvalidator_p.cpp" |
| 233 | |