yry
2025-09-09 5ea3f03865e7b0338e249ec704ec93bf1f4b426b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* 自定义头文件 */
#include "textvalidator.h"
 
/* 系统头文键 */
#include <QDebug>
 
/* 预处理指令·编译指示 */
#pragma execution_character_set("utf-8")
 
 
/* 构造函数 */
TextValidator::TextValidator()
{
 
}
 
/* 析构函数 */
TextValidator::~TextValidator()
{
 
}
 
/* 用户名格式检查 */
bool TextValidator::isValidUsername(const QString username)
{
    /* 长度检查 */
    if((username.length() < 4) || (username.length() > 10)){
        //qDebug()<<"用户名长度违规。。";
        return false;
    }
 
    /* 内容检查 */
    QRegularExpression regex("^[a-zA-Z0-9_\\x{4e00}-\\x{9fa5}]+$");//PCRE(Perl兼容正则表达式)
    if (!regex.isValid()) {
            qDebug() << "正则表达式无效【格式错误,不可用】:" << regex.errorString();
        }
    QRegularExpressionMatch match = regex.match(username);
 
    if(match.hasMatch()){//匹配返回true,失败返回false
        //qDebug()<<"匹配成功";
        return true;
    }else {
        //qDebug()<<"匹配失败";
        return false;
    }
}
 
/* 密码格式检查 */
bool TextValidator::isValidPassword(const QString password)
{
    /* 长度检查 */
    if((password.length() < 5) || (password.length() > 20)){
        //qDebug()<<"密码长度违规。。";
        return false;
    }
 
    /* 内容检查 */
    QRegularExpression regex("^[a-zA-Z0-9_.]+$");//PCRE(Perl兼容正则表达式)
    if (!regex.isValid()) {
            qDebug() << "正则表达式无效:" << regex.errorString();
        }
    QRegularExpressionMatch match = regex.match(password);
 
    if(match.hasMatch()){//匹配返回true,失败返回false
        //qDebug()<<"匹配成功";
        return true;
    }else {
        //qDebug()<<"匹配失败";
        return false;
    }
}
 
/* 邮箱地址格式检查 */
bool TextValidator::isValidEmail(const QString email)
{
    /* 长度检查 */
    if (email.length() > 254 || email.length() < 6) {
            //qDebug()<<"邮箱长度违规。。";
            return false;
        }
 
    /* 内容检查 */
    QRegularExpression regex(R"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$)");//PCRE(Perl兼容正则表达式)
    if (!regex.isValid()) {
            qDebug() << "正则表达式无效:" << regex.errorString();
        }
    QRegularExpressionMatch match = regex.match(email);
 
    if(match.hasMatch()){//匹配返回true,失败返回false
        //qDebug()<<"匹配成功";
        return true;
    }else {
        //qDebug()<<"匹配失败";
        return false;
    }
}
 
/* 电话号码格式检查 */
bool TextValidator::isValidTelephone(const QString telephone)
{
    /* 长度检查 */
    if (telephone.length() != 11) {
            //qDebug()<<"电话号码长度违规。。";
            return false;
        }
 
    /* 内容检查 */
    QRegularExpression regex(R"(^1[3-9]\d{9}$)");//PCRE(Perl兼容正则表达式)
    if (!regex.isValid()) {
            qDebug() << "正则表达式无效:" << regex.errorString();
        }
    QRegularExpressionMatch match = regex.match(telephone);
 
    if(match.hasMatch()){//匹配返回true,失败返回false
        //qDebug()<<"匹配成功";
        return true;
    }else {
        //qDebug()<<"匹配失败";
        return false;
    }
}