How to make lineEdit only accept string?
-
Hello everyone,
I have been trying to make my line edits only accept alphabet characters(no numbers, no special characters) using QRegularExpression but I have not been succeed so far.
This is my code:QRegularExpression re("[A-Z][a-z]"); QRegularExpressionValidator v(re, this); ui->lineEditFirstName->setValidator(&v); ui->lineEditLastName->setValidator(&v);
But somehow I can still type numbers and special characters in my line edit which is not what I want.
Thank you for reading and have a good day! -
@tjktak1002 said in How to make lineEdit only accept string?:
QRegularExpressionValidator v(re, this);
Your validator is a local variable and is destroyed as soon as it gets out of scope...
-
ok one solution to my problem that I have found (allow all alphabet characters and whitespace)
QRegularExpressionValidator* v = new QRegularExpressionValidator(QRegularExpression(R"(^[a-zA-Z- ]*)")); ui->lineEditFirstName->setValidator(v); ui->lineEditLastName->setValidator(v);
-
@tjktak1002 said in How to make lineEdit only accept string?:
all alphabet characters
My alphabet has umlauts... ->
\w
-
@Christian-Ehrlicher said in How to make lineEdit only accept string?:
umlauts.
hey this one works for my french keyboard so maybe it can help you a bit
QRegularExpressionValidator* v = new QRegularExpressionValidator(QRegularExpression(R"(^[a-zA-ZÀ-ÿ-. ]*$)"));
-
@tjktak1002 said in How to make lineEdit only accept string?:
hey this one works for my french keyboard so maybe it can help you a bit
I know what to do - I just wanted to tell you that your regex doesn't accept all characters but only a few.
-
@Christian-Ehrlicher oh ok haha :D Thanks for reminding me tho