TextInput validators in Qt Qml
Last updated
Last updated
This blog is really about TextInput validators:
InputMask
IntValidator
DoubleValidator
RegExpValidator
InputValidator (in this example, we will show you how to say βNoβ to unicorns!)
The first 4 is from Qtβs runtime. The last is from AppStudioβs AppFramework.
View full InputMaskTest.qml on GitHub Gist.
InputMask is a property on TextInput components such as TextField. It allows us to quickly define acceptable text input. In the above example we allow 6 character license plates that begin with 3 digits, followed by a dash and finishing with 3 characters. When acceptable input is entered the text field changes from βredβ to βgreenβ and the user can submit the input.
The InputMask has the following features:
When the field is βemptyβ youβll see placeholders for symbols like hyphen (β-β), you will not see the placeholder text
When you type, you do not need to type the fixed symbols, e.g. hyphen (β-β)
The code is very short
https://doc.qt.io/qt-5/qml-qtquick-textinput.html#inputMask-prop
https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop
View full IntValidatorTest.qml on GitHub Gist.
IntValidator is a QML component that can be assigned as a TextInput validator. It allows us to quickly define acceptable numerical input. In above example we allow any integer between 25 and 75. When acceptable input is entered the text field changes from βredβ to βgreenβ and the user can submit the input.
The IntValidator has the following features:
When the field is empty you can see the placeholder text
You cannot type more than the number of characters hinted by the IntValidator (e.g. in the above example you cannot type more than two digits)
https://doc.qt.io/qt-5/qml-qtquick-intvalidator.html
View full DoubleValidatorTest.qml on GitHub Gist.
DoubleValidator is a QML component that can be assigned as a TextInput validator. It allows us to quickly define acceptable numerical input. In above example we allow any number between 25.0 and 75.0. When acceptable input is entered the text field changes from βredβ to βgreenβ and the user can submit the input.
The DoubleValidator has the following features:
When the field is empty youβll see the placeholder text
You can type in any length (because decimal points are allowed)
https://doc.qt.io/qt-5/qml-qtquick-doublevalidator.html
View full RegExpValidatorTest.qml on GitHub Gist.
RegExpValidator is a QML component that can be assigned as a TextInput validator. It allows us to quickly define acceptable input. In the above example, we allow for license plates that begin with a 3 digit number, followed by a dash and ending with 3 letters.
RegExpValidator has the following features:
When the field is empty youβll see the placeholder text (different than InputMask)
You must type the hyphen, it wonβt be automatically typed for you (different than InputMask)
The regular expression is significantly more complex to implement and maintain
https://doc.qt.io/qt-5/qml-qtquick-regexpvalidator.html
View full InputValidatorTest.qml on GitHub Gist.
InputValidator is a QML component that can be assigned as a TextInput validator. It allows us to supply a Javascript function to be the validator. The Javascript function receives both the original string and position as input. It can return an input state (InputValidator.Invalid, InputValidator.Intermediate or InputValidator.Acceptable). It can also return an object that contains an updated version of the string, position and state. Returning an updated string allows us to implement auto correction. In the above example we forbid the user from typing in βunicornβ. Itβs not allowed! We also forbid the user from typing βcanineβ. If they attempt to type βcanineβ it gets auto corrected as βdogβ. The sentence will never be full accepted until the user finishes it with a full stop β.β.
InputValidator has the following features:
When the field is empty youβll see the placeholder text
You can prohibit certain input (e.g. we can come up with a word blacklist, e.g. censor offensive words)
You can correct / fix input (i.e. implement auto correct)
You have full control over the experience
Takes more effort to implement your rules
https://doc.arcgis.com/en/appstudio/api/reference/framework/qml-arcgis-appframework-inputvalidator/