使用C ++和Qt进行密码验证

时间:2015-05-29 22:23:43

标签: c++ qt qt-creator

我正在使用C ++在Qt中构建一个密码验证程序,但是我遇到了一些问题,使它与Qt一起正常工作。

显然Qt没有使用isalpha功能,因此我无法判断某个角色是否为alpha。使用它会导致错误No matching function for call to 'isalpha'我无法找到一个好的替代品。

即使Qt支持No matching member function for call to 'isUpper' and 'isDigit',我也会收到错误isUpper,与isalpha不同。

我正在尝试移植在纯C ++中完美运行的代码。这就是代码:

int i;
char password[30];
int lower, upper, number, symbol;
lower = upper = number = symbol = 0;

cout << "Enter your password" << endl;
cin >> password;

int len = strlen(password);

for (i=0; i<len; i++)
{
    if(isalpha(password[i]))
    {
        if(isupper(password[i]))
        {
            upper++;
        }
        else
        {
            lower++;
        }
    }
    else if(isdigit(password[i]))
    {
        number++;
    }
    else
    {
        symbol++;
    }
}

if (upper >= 1 && lower >= 1 && number >= 1 && symbol >= 1 && len >=6)
{
    printf("Your password is good!");
}
if (upper < 1)
{
    printf("You need an uppercase letter \n");
}
if (lower < 1)
{
    printf("You need a lowercase letter \n");
}
if (number < 1)
{
    printf("You need a number \n");
}
if (symbol < 1)
{
    printf("You need a symbol \n");
}
if (len < 6)
{
    printf("Your password must be at least 6 characters \n");
}

我在Qt中设置它的方式几乎完全相同,但是我无法在没有is alphaisUpper错误的错误的情况下运行它:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QRegExp>
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    int i;
    QString username = ui->lineEdit->text();
    QString password = ui->lineEdit_2->text();
    int usernameLen = ui->lineEdit->text().length();
    int passwordLen = ui->lineEdit_2->text().length();
    // QLabel valLabel = ui->label_5;
    QString valMsg = ui->label_5->text();
    int upper;
    int lower;
    int digit;
    int symbol;

    if(usernameLen < 8)
    {
        ui->label_5->setText("Your username must be at least 8 characters\n");
    }
    if(passwordLen < 8)
    {
        ui->label_5->setText(valMsg + "Your password must be at least 8 characters\n");
    }

    for(i=0; i<usernameLen; i++)
    {
        if(isalpha(username[i]))
        {
            if(QChar::isUpper(username[i]))
            {
                upper++;
            }
            else
            {
                lower++;
            }
        }
        else if (QChar::isDigit(username[i]))
        {
            digit++;
        }
        else
        {
            symbol++;
        }
    }
    if (upper >= 1 and lower >= 1 and digit >= 1 and symbol >= 1 and usernameLen >=6)
    {
        ui->label_5->setText(valMsg + username + "Your password is good");
    }
    if (upper < 1)
    {
        ui->label_5->setText(valMsg + username + "Password must contain at least one uppercase letter\n");
    }
    if (lower < 1)
    {
        ui->label_5->setText(valMsg + username + "Password must contain at least one lowercase letter\n");
    }
    if (digit < 1)
    {
        ui->label_5->setText(valMsg + username + "Password must contain at least one number\n");
    }
    if (symbol < 1)
    {
        ui->label_5->setText(valMsg + username + "Password must contain at least one symbol\n");
    }
}

1 个答案:

答案 0 :(得分:0)

为什么不试试这个:

// ...

QString userName = "UserName1";
for ( const auto& character : userName )
{
    if ( character.isUpper() )
    {
        upper++;
    }
    else if ( character.isLower() )
    {
        lower++;
    }
    else if ( character.isDigit() )
    {
        digit++;
    }
    else
    {
        symbol++;
    }
}

// ...

密码类似。