邮箱验证:
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")
QRegExp passwordREX("[A-Z0-9a-z_]{6,9}"); passwordREX.setPatternSyntax(QRegExp::RegExp);
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(); }