Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Special Character Validation
Qt 6.11 is out! See what's new in the release blog

Special Character Validation

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I want to do special character validation.
    When creating a folder, you need to validate the name.
    There are special characters that are limited when creating folders in Windows.

    QString v("[^\\\/:*?\"<>|]");
    this->setValidator(new QRegExpValidator(QRegExp(v)));
    

    I wrote the code like this, but no characters are input.
    Is there a way to disable only special characters?

    I 1 Reply Last reply
    0
    • I IknowQT

      I want to do special character validation.
      When creating a folder, you need to validate the name.
      There are special characters that are limited when creating folders in Windows.

      QString v("[^\\\/:*?\"<>|]");
      this->setValidator(new QRegExpValidator(QRegExp(v)));
      

      I wrote the code like this, but no characters are input.
      Is there a way to disable only special characters?

      I Offline
      I Offline
      IknowQT
      wrote on last edited by
      #2

      @IknowQT

      	QString v("[^\\\\/`~!@#$%^&*:*?\"<>|.]*");
      	this->setValidator(new QRegExpValidator(QRegExp(v)));
      
      JonBJ 1 Reply Last reply
      0
      • I IknowQT

        @IknowQT

        	QString v("[^\\\\/`~!@#$%^&*:*?\"<>|.]*");
        	this->setValidator(new QRegExpValidator(QRegExp(v)));
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @IknowQT
        It doesn't matter, but why do you start your regular expression with [^\\\\? That is 2 literal backslashes, the second one achieves nothing inside [^. For readability/simplicity, might as well be "[^\\/...]". You also have * listed twice. And at least some of your excluded characters are allowed in Windows file/directory names, such as, say, ! or @. You may forbid them if that is what you wish, but just be aware they are not forbidden characters. A list of genuinely illegal characters (there are only 9 disallowed punctuation ones --- oh, that is what you had in your first attempt) can be found at e.g. https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions. There are also further rules than just illegal characters, e.g. "The filename can't end with a space or a period".

        It is a shame you persist with QRegExp stuff. Unless you are using a pretty old Qt version, you would be better moving to QRegularExpression, and will require to do so if you upgrade.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved