5

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 me@me.com, the error message appear. I also tried using

int regMat = mailREX.indexIn(mail.text());

and it didnt work.

Thanks in advance

Vordok
  • 340
  • 1
  • 5
  • 16
  • 2
    possible duplicate of [How to use a regular expression to validate an email addresses?](http://stackoverflow.com/questions/201323/how-to-use-a-regular-expression-to-validate-an-email-addresses) – johnsyweb Feb 01 '12 at 19:25

2 Answers2

12

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("me@me.com");

prints true

Lol4t0
  • 12,444
  • 4
  • 29
  • 65
9

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();
}
nerdoc
  • 1,044
  • 10
  • 28