TextField with RegExpValidator


  • Qt Champions 2016

    I want to set a validator which allows following input into a (QtQuickControls2) TextField:

    nnn/nnn
    where nnn can have 2 or 3 digits and a minimum of 30 and a maximum of 300
    per ex

    80/120
    110/90

    RegEx is very powerful, but I'm always fighting with the syntax ;-)

    can someone help with the exact syntax ?

    validator: RegExpValidator{regExp: "???"}
    

    thx


  • Moderators

    @ekkescorner

    Do you mean the regExp syntax?

    Derived from c++ probably more "\\d{2,3}/\\d{2,3}".
    This is tested with the regExp example in C++. Since I do not know Qt Quick, I do not know the requirements for backslashes and their duplication.

    Note: this satisfies also "80/90" and "100/120". Possibly this is already enough for you.


  • Moderators

    Playing a bit more with the example
    it would be: "^(\\d{2,2}/\\d{3,3})|(\\d{3,3}/\\d{2,2})$" and allows only digits and forward as in your description.


  • Lifetime Qt Champion

    Hi,

    While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
    Something like /\d{2,3}\/\d{2,3}/ should be enough.


  • Qt Champions 2016

    @SGaist said in TextField with RegExpValidator:

    Hi,

    While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
    Something like /\d{2,3}\/\d{2,3}/ should be enough.

    so to check min and max I can split the text by '/' and check both numbers

    thx for helping - will try it out


  • Qt Champions 2016

    @SGaist said in TextField with RegExpValidator:

    Hi,

    While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
    Something like /\d{2,3}\/\d{2,3}/ should be enough.

    did this:

    validator: RegExpValidator{regExp: "/\d{2,3}\/\d{2,3}/"}
    

    but got an error:

    Invalid property assignment: regular expression expected; use /pattern/ syntax
    

    regexp mentioned @koahnig also don't work


  • Moderators

    @ekkescorner Try without quotes.


  • Qt Champions 2016

    @p3c0 thx it works without the quotes

    next question:
    if I want to allow
    one character of

    /
    .
    ,
    

    how must the regexp be constructed ?

    reason:
    on Android and iOS want to use virtual numeric keyboard where I don't have a '/'
    but I can enter . on Android and , on iOS (german locale)
    so I do a text.replace changing . or , into /
    but using the RegExp only allowing a / as separator I cannot enter . or ,

    thx again


  • Qt Champions 2016

    @ekkescorner found it:

    /\d{2,3}[\.\,\/]\d{2,3}/
    

    thx all for help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.