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
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
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...![]()
Oh my God, what an expression.... How about:
Qt Code:
public: if(input.isEmpty()) return Acceptable; int s = slist.size(); if(s>4) return Invalid; bool emptyGroup = false; for(int i=0;i<s;i++){ bool ok; if(slist[i].isEmpty()){ emptyGroup = true; continue; } int val = slist[i].toInt(&ok); if(!ok || val<0 || val>255) return Invalid; } if(s<4 || emptyGroup) return Intermediate; return Acceptable; } };To copy to clipboard, switch view to plain text mode
Last edited by wysota; 24th March 2007 at 00:48. Reason: Corrected the code
apat (30th June 2007), arturo182 (1st June 2009), bruccutler (24th March 2007), coolboy123 (17th May 2008), deepakn (28th June 2007), MarkoSan (10th June 2008)
This worked perfectly! Thanks so much! It took me a while to get back to it, but this really did the job!
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.
Bookmarks