I need to validate that the text entered in a qlineedit has the form of the regexp, I tried to use this:
void MainWindow::checkReg( QLineEdit& mail, const QCheckBox& skip, string type )
{
if(type == "mail")
{
if( skip.checkState() != 2)
{
QRegExp mailREX("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
mailREX.setCaseSensitivity(Qt::CaseInsensitive);
mailREX.setPatternSyntax(QRegExp::Wildcard);
bool regMat = mailREX.exactMatch(mail.text());
if(regMat == false)
{
QMessageBox *message = new QMessageBox(this);
message->setWindowModality(Qt::NonModal);
message->setText("Inserte los datos en el formato correcto");
message->setStandardButtons(QMessageBox::Ok);
message->setWindowTitle("MainWindow");
message->setIcon(QMessageBox::Information);
message->exec();
this->ok = 0;
mail.clear();
}
else
this->ok = 1;
}
}
}
but every mail mail I entered like [email protected], the error message appear. I also tried using
int regMat = mailREX.indexIn(mail.text());
and it didnt work.
Thanks in advance
Please do NOT use regex to “validate” email addresses. You're almost certainly guaranteed to get it wrong, as the RFC's for what constitutes a “valid” email address are extremely complex.
Email Regex – Simple Validation.+)@(\S+)$ to validate an email address. It checks to ensure the email contains at least one character, an @ symbol, then a non whitespace character.
Why did you set pattern syntax to wildcard
? Your code works (assuming you understand, that your regexp itself is simplified) with RegExp
pattern syntax:
QRegExp mailREX("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
mailREX.setCaseSensitivity(Qt::CaseInsensitive);
mailREX.setPatternSyntax(QRegExp::RegExp);
qDebug() << mailREX.exactMatch("[email protected]");
prints true
If you want to create a real QValidator for reusing in e.g. QLineEdits, you also have to check for Intermediate email adress strings, else nothing will be accepted unless you COPY&PASTE an email address into the edit.
Here is an example for an EmailValidator:
The emailvalidator.h file:
#ifndef EMAILVALIDATOR_H
#define EMAILVALIDATOR_H
#include <QValidator>
QT_BEGIN_NAMESPACE
class QRegExp;
QT_END_NAMESPACE
class EmailValidator : public QValidator
{
Q_OBJECT
public:
explicit EmailValidator(QObject *parent = 0);
State validate(QString &text, int &pos) const;
void fixup(QString &text) const;
private:
const QRegExp m_validMailRegExp;
const QRegExp m_intermediateMailRegExp;
};
#endif // EMAILVALIDATOR_H
And the emailvalidator.cpp file:
#include "emailvalidator.h"
EmailValidator::EmailValidator(QObject *parent) :
QValidator(parent),
m_validMailRegExp("[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}"),
m_intermediateMailRegExp("[a-z0-9._%+-]*@?[a-z0-9.-]*\\.?[a-z]*")
{
}
QValidator::State EmailValidator::validate(QString &text, int &pos) const
{
Q_UNUSED(pos)
fixup(text);
if (m_validMailRegExp.exactMatch(text))
return Acceptable;
if (m_intermediateMailRegExp.exactMatch(text))
return Intermediate;
return Invalid;
}
void EmailValidator::fixup(QString &text) const
{
text = text.trimmed().toLower();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With