Results 1 to 20 of 27

Thread: Ip Address Validation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    91
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    21

    Default Re: Ip Address Validation

    So - what is the right way to do this? I think I will set it aside, since I'm sure someone out there has solved this problem with Qt.
    - BRC

  2. #2
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    13
    Thanked 6 Times in 5 Posts

    Default Re: Ip Address Validation

    I think you could do it with the combination of a masked line edit, and a QRegExpValidator that checks for a regex like this: (?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)(?:\.(?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)){3}

    That said, I haven't tried this myself, so who knows...

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Ip Address Validation

    Oh my God, what an expression.... How about:
    Qt Code:
    1. class IP4Validator : public QValidator {
    2. public:
    3. IP4Validator(QObject *parent=0) : QValidator(parent){}
    4. void fixup(QString &input) const {}
    5. State validate(QString &input, int &pos) const {
    6. if(input.isEmpty()) return Acceptable;
    7. QStringList slist = input.split(".");
    8. int s = slist.size();
    9. if(s>4) return Invalid;
    10. bool emptyGroup = false;
    11. for(int i=0;i<s;i++){
    12. bool ok;
    13. if(slist[i].isEmpty()){
    14. emptyGroup = true;
    15. continue;
    16. }
    17. int val = slist[i].toInt(&ok);
    18. if(!ok || val<0 || val>255) return Invalid;
    19. }
    20. if(s<4 || emptyGroup) return Intermediate;
    21. return Acceptable;
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 24th March 2007 at 00:48. Reason: Corrected the code

  4. The following 6 users say thank you to wysota for this useful post:

    apat (30th June 2007), arturo182 (1st June 2009), bruccutler (24th March 2007), coolboy123 (17th May 2008), deepakn (28th June 2007), MarkoSan (10th June 2008)

  5. #4
    Join Date
    Jan 2007
    Posts
    91
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    21

    Talking Re: Ip Address Validation

    This worked perfectly! Thanks so much! It took me a while to get back to it, but this really did the job!

  6. #5
    Join Date
    Jan 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 5 Times in 5 Posts
    Wiki edits
    2

    Default Re: Ip Address Validation

    Quote Originally Posted by Jimmy2775 View Post
    I think you could do it with the combination of a masked line edit, and a QRegExpValidator that checks for a regex like this: (?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)(?:\.(?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)){3}

    That said, I haven't tried this myself, so who knows...
    I know that problem is already solved, but... it's definetely too long regexp. I love the power of regular expressions, so here is my shorter version (w/o non-capturing parentheses, for increased readability), where each octet range from 0 to 255:

    ^0*(2(5[0-5]|[0-4]\d)|1?\d{1,2})(\.0*(2(5[0-5]|[0-4]\d)|1?\d{1,2})){3}$

    QRegExpValidator of course doesn't need ^ and $ assertions.

Similar Threads

  1. how to read pc's network IP address
    By wei243 in forum Qt Programming
    Replies: 12
    Last Post: 8th January 2010, 17:59
  2. MULTICAST with QT 4.1 and above.
    By jlarsj in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2007, 13:45
  3. How to determine ip address of remote host?
    By nopalot in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2006, 22:18
  4. Problem with mask and validation
    By gunhelstr in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 09:07
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 23:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.