'How to only allow the user type numbers and comma in the qml textField?

I want print the value of textField, but I want that the user only can type numbers, "," and /

TextField {
    id: textField1
    implicitWidth: 200
    implicitHeight: 30
}
Button{
    onClicked:{console,log(textField1.text)}
}


Solution 1:[1]

To answer your question, here's the working solution below.

 TextField {
    id: textField1
    implicitWidth: 200
    implicitHeight: 30
    validator: RegExpValidator{regExp: /^[0-9,/]+$/}
}

Button{
    onClicked:{console.log(textField1.text)}
}

To prevent a user from entering something use a text validator in Qt Found here. It makes use of RegEx so u have more control.

by doing this you completely prevent a user from being able to enter the characters you dont want.

Solution 2:[2]

on android and ios devices use

inputMethodHints : Qt.ImhFormattedNumbersOnly

in TextField to Open Keyboard on Only number input That is includes decimal point and minus sign. and Other inputMethodHints TextFiled QML

and use Regex in Validator for add Restrictions on Input.

Example:

TextField{
    validator: RegExpValidator{regExp: /[0-9,/]*/}
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NhlalukoG Masingi
Solution 2