emailvalidator.cpp 895 Bytes
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
2
3
4
5
  SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
  SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>

  SPDX-License-Identifier: LGPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
6
7
8
9
10
11
12
13
14
*/

#include "emailvalidator.h"

#include <KEmailAddress>
#include <QRegularExpression>

using namespace PimCommon;

Laurent Montel's avatar
Laurent Montel committed
15
16
EmailValidator::EmailValidator(QObject *parent)
    : QValidator(parent)
Laurent Montel's avatar
Laurent Montel committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
}

QValidator::State EmailValidator::validate(QString &str, int &pos) const
{
    Q_UNUSED(pos);

    if (KEmailAddress::isValidSimpleAddress(str)) {
        return QValidator::Acceptable;
    }
    QRegularExpression re(QStringLiteral("\\s"));
    QRegularExpressionMatch match = re.match(str);
    if (match.hasMatch()) {
        return QValidator::Invalid;
    }
    return QValidator::Intermediate;
}

void EmailValidator::fixup(QString &str) const
{
    str = str.trimmed();
}