Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: Ip Address Validation

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

    Default Ip Address Validation

    I've checked a lot of sites, but haven't found a definitive IP Address validation routine. I'm using the IPAddress mask, but is there a better way to validate the address than capturing the finishedEditing signal and doing it via code (x > 0 && x< 256)?

    Also, how do I tell Qt to keep the focus in the current field if the entry is in error? lineEdit.SetFocus() gives me recursion problems with my finishedEditing signal.

    - BRC

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

    Default Re: Ip Address Validation

    Quote Originally Posted by bruccutler View Post
    ...is there a better way to validate the address than capturing the finishedEditing signal and doing it via code (x > 0 && x< 256)?

    Also, how do I tell Qt to keep the focus in the current field if the entry is in error? lineEdit.SetFocus() gives me recursion problems with my finishedEditing signal.
    A QRegExpValidator might be what you're looking for in both cases here.

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

    Default Re: Ip Address Validation

    No - reg expression will check the values, but won't do a full check for 1-255 for all values. At least not one that I've ever seen.
    - Bruce

  4. #4
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ip Address Validation

    Quote Originally Posted by bruccutler View Post
    No - reg expression will check the values, but won't do a full check for 1-255 for all values.
    You could, but it would be a fairly complex regular expression.

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

    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

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

    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...

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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; 23rd March 2007 at 23:48. Reason: Corrected the code

  8. 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)

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

    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!

  10. #9
    Join Date
    Jan 2008
    Location
    Warsaw, Poland
    Posts
    26
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    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.

  11. #10

    Default Re: Ip Address Validation

    But how to combine with setMask("000.000.000.000;")?

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

    Default Re: Ip Address Validation

    You can't. But the validator does almost the same job, especially if you implement its fixup method.

  13. #12
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Ip Address Validation

    Hi I've the same problem...I've to implement a dialog like WinXp TCP/IP Properties of Network Connections->MyConnection Properties:



    How use IP4Validator with that kind of masks (IP Address and Subnet Mask) and how to implemet them?
    Last edited by fruzzo; 7th May 2008 at 14:41.

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

    Default Re: Ip Address Validation

    You can cheat by having four frameless line edits and three labels grouped in a horizontal layout and with overriden key events to move to the next group when the previous one is filled or when arrow keys are used. That's probably what the mentioned dialog does, too.

  15. #14
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Ip Address Validation

    Quote Originally Posted by wysota View Post
    You can cheat by having four frameless line edits and three <a href="http://www.ntsearch.com/search.php?q=labels&v=56">labels</a> grouped in a horizontal layout and with overriden key events to move to the next group when the previous one is filled or when arrow keys are used. That's probably what the mentioned dialog does, too.
    Ah ok thanks...that's what I suppose!

    And...about reimplementing the fixup function? what's the idea?

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

    Default Re: Ip Address Validation

    That you can change an invalid entry to a valid entry. For instance decimal point in some countries is the dot character and in others it is comma. Thanks to fixup you can detect a comma instead of a dot (or the other way round) and change it to something you consider valid.

  17. #16
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Ip Address Validation

    Quote Originally Posted by wysota View Post
    That you can change an invalid entry to a valid entry. For instance decimal point in some countries is the dot character and in others it is comma. Thanks to fixup you can detect a comma instead of a dot (or the other way round) and change it to something you consider valid.

    ah ok thanks...and about fixup to format string like inputmask?

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

    Default Re: Ip Address Validation

    You can do there whatever you like.

  19. #18
    Join Date
    Jul 2008
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ip Address Validation

    can u please write the same code of urs for IP address validation.I cannt understand the code written here .

    please help me out .

    thanks

  20. #19
    Join Date
    Jul 2008
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ip Address Validation

    how can i use fix up in Ip address validation

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

    Default Re: Ip Address Validation

    You get an invalid address as an argument and you need to change it to a valid one, for example change all values greater than 255 to 255.

Similar Threads

  1. how to read pc's network IP address
    By wei243 in forum Qt Programming
    Replies: 12
    Last Post: 8th January 2010, 16:59
  2. MULTICAST with QT 4.1 and above.
    By jlarsj in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2007, 12:45
  3. How to determine ip address of remote host?
    By nopalot in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2006, 21:18
  4. Problem with mask and validation
    By gunhelstr in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 08:07
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.