[SOLVED] How can a lineedit accept only (ASCII alphanumeric character required. A-Z, a-z, 0-9.)
-
I am seeing "this":http://doc.qt.nokia.com/latest/qlineedit.html#inputMask-prop but i can't understant what to put at the inputmask at qtdesigner..
!http://img689.imageshack.us/img689/5366/screenshot1oa.png(screen)!
This isn't correct. So how to accept only A-Z,a-z and 0-9?
-
to solve your problem, you may use the "QRegExpValidator":http://doc.qt.nokia.com/4.7/qregexpvalidator.html
@
lineEdit->setValidator(new QRegExpValidator( QRegExp("\w"), this ));
@I have used recently the QRegExpValidator instead of inputMask for very same reason. Please verify the validity of the RegExp I defined.
-
Hmm as seen "here":http://developer.qt.nokia.com/forums/viewthread/8432 with this it works fine.
@ ui->lineEdit->setValidator(new QRegExpValidator( QRegExp("[A-Za-z0-9_]{0,255}"), this ));
@So adding {0,255} let me put more than i letter. But why? is this correct like this?
-
Well, I'm not a regexp specialist but that's how regexp work. [A-Za-z0-9_] only matches one character.
If you want to match an undefined amount of characters, you'll have to use one of these metacharacteres :
- '*' Matches zero or more times ;
- '+' Matches one or more times.
For example :
@ui->lineEdit->setValidator(new QRegExpValidator( QRegExp("[A-Za-z0-9_]+"), this ));@
If I'm not wrong, you can also use :
@[A-Za-z0-9_]{1,}@