2018년 4월 26일 목요일

MFC password regular expression check task second

To use the first regular expression, check the link below.
MFC regular expression

The same pattern is also included in the sources below.
Additional code to recognize patterns for repeated numbers, letters, and consecutive numbers or letters
It is a supplemented source. ^^


 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
CString sPasswd;
// sPasswd = "1234abcdccc";

GetDlgItemText(IDC_EDIT_PWD, sPasswd);

int nCount = sPasswd.GetLength();
//Length check
if (nCount < 8 && nCount > 13)
{
    AfxMessageBox("Password length is 8 to 20 digits.", MB_OK, 0);
    return;
}

bool bEngFlag=false, bNumFlag=false, bExtFlag=false;

char szExt[] = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
// check in English
// Numeric check
// special character check
for (int i=0; i<nCount; i++)
{
    char cChk = sPasswd.GetAt(i);
    if (!bEngFlag)
        bEngFlag = isalpha(cChk);
    if (!bNumFlag)
        bNumFlag = isdigit(cChk);
    if (!bExtFlag)
    {
        for (int j=0; j<sizeof(szExt); j++)
        {
            if (cChk == szExt[j])
            {
                bExtFlag = true;
                break;
            }
        }
    }
}

// Use more than 4 consecutive characters
// Repeated character / number check.
int nRepeatCnt=0, nConCnt=0;
for (int i=0; i<nCount; i++)
{
    char cChk1 = sPasswd.GetAt(i);
    char cNextChk = cChk1 + 1;
    char cChk2 = sPasswd.GetAt(i+1);
    
    if(cNextChk == cChk2)
        nConCnt++;
    else
        nConCnt = 1;

    if(cChk1 == cChk2)
        nRepeatCnt++;
    else
        nRepeatCnt = 1;

    if(nRepeatCnt > 3) break;
    if(nConCnt > 3) break;
}

if (!bEngFlag || !bNumFlag || !bExtFlag)
{
    AfxMessageBox("The password is invalid.");
    return;
}
else if(nConCnt > 3)
{
    AfxMessageBox("Do not use more than 4 consecutive characters or sequential numbers in the password.");
}
else if(nRepeatCnt > 3)
{
    AfxMessageBox("Do not use more than 4 repeated letters / numbers in the password.");
}
else
{
    AfxMessageBox("Password is valid.");
}