/* 自定义头文件 */ #include "textvalidator.h" /* 系统头文键 */ #include /* 预处理指令·编译指示 */ #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; } }