QML TextInput element's validation
-
Hello Folks,
I am facing a problem while using validator property with TextInput. I am able to validate input but i want that if user is entering data which is out of validation range she will be warned with a popup.
Here is my code:@TextInput {
id: inputBox
objectName: "InputBox"
horizontalAlignment: TextInput.AlignHCenter
color: "black"
font.family: "Arial"
anchors.fill: parent
anchors.leftMargin: 5
anchors.rightMargin: 5
anchors.topMargin: 5
font.pixelSize:13
maximumLength: 4
validator: DoubleValidator{bottom: -5.0; top: 5.0; decimals: 1; notation: DoubleValidator.StandardNotation }
}@Here i am restricting user to enter only values from -5.0 to 5.0. If user is entering any values out of this range a popup should appear.
Thanks in advance.
Let me know if more details are required or problem is not clear to you.Regards,
Punitedit: corrected code little bit
-
I think your problem here is that when a validator is set, the user cannot enter illegal values. Pressing the keys will have no effect. If you're looking for reactive validator, you'll need to handle the textChanged signal with your own validator like so:
@
TextInput {
id: inputBox
objectName: "InputBox"
horizontalAlignment: TextInput.AlignHCenter
color: "black"
font.family: "Arial"
anchors.fill: parent
anchors.leftMargin: 5
anchors.rightMargin: 5
anchors.topMargin: 5
font.pixelSize:13
maximumLength: 4onTextChanged: { if (parseFloat(text) < -5 || parseFloat(text) > 5) { console.log("Invalid input"); //Display whatever warning here inputBox.color = "red"; } else { console.log("Input good"); //Hide warning here inputBox.color = "black"; } } }
@