PDA

View Full Version : IP adresses with QRegExp (Qt3)



jlbrd
27th January 2006, 08:29
Hello,

I want to define a mask for QLineEdit to match IP addresses. I want to prohibit the 0 front the number entered. With QRegExp how to allow 1 but not 01 nor 001, 21 but not 021 etc.

Thanks,

wysota
27th January 2006, 09:00
set input mask to "D00.D00.D00.D00"

Of course this doesn't protect from entering an invalid IP, like 999.999.999.999 and also forbids from entering 0.0.0.0 or 192.168.0.1

So maybe it would be better to use a regexp validator instead of input mask and/or substitute those 'D's with '0's.

jacek
27th January 2006, 09:38
With QRegExp how to allow 1 but not 01 nor 001, 21 but not 021 etc
It should be something like this:
(?:(?:[01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}(?:[01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])

jlbrd
31st January 2006, 14:43
Here a solution:



QRegExpValidator* ValIPAddr;
QRegExpValidator* ValNetMask;
// match the Bytes can be from 0-199 or 200-249 or 250-255 but not a number with one 0 at the beginning like 001 or 020
QString Byte = "(?!0[0-9])(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
QRegExp rxip;
rxip.setPattern("^" + Byte + "\\." + Byte + "\\." + Byte + "\\." +
Byte + "$");
ValIPAddr = new QRegExpValidator(rxip, 0);
QRegExp rxnetmask;
rxnetmask.setPattern("^255\\.255\\.255\\.(?:255|254|252|248|240|224|192| 128|0)$|^255\\.255\\.(?:254|252|248|240|224|192|12 8|0)\\.0$|^255\\.(?:254|252|248|240|224|192|128|0) \\.0\\.0$|^(?:254|252|248|240|224|192|128|0)\\.0\\ .0\\.0$");
ValNetMask = new QRegExpValidator(rxnetmask, 0);
ip->setValidator(ValIPAddr);
mask->setValidator(ValNetMask);