要求:
- 用户名支持汉字、数字、字母、下划线及其组合,长度限制为二至八位
- 密码必须由数字、字母、下划线组成,长度至少为六位
CString strName, strPSW;
GetDlgItem(IDC_EUSER)->GetWindowText(strName);
GetDlgItem(IDC_EPSW)->GetWindowText(strPSW);
wchar_t ch1[] = L"^[a-zA-Z0-9\u4e00-\u9fa5_]{2,8}$";
wstring parten1(ch1);
std::wregex re_1(parten1);
wchar_t ch2[] = L"^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[_])[A-Za-z0-9_]{6,}$";
wstring parten2(ch2);
std::wregex re_2(parten2);
wstring str_name = strName;
wstring str_pwd = strPSW;
if (!regex_match(str_name, re_1))
{
//用户名不符合要求
AfxMessageBox(L"输入用户名必须是汉字、数字、字母、下划线及其组合,长度为二至八位");
GetDlgItem(IDC_EUSER)->SetFocus();
}
if (!regex_match(str_pwd, re_2))
{
//密码不符合要求
AfxMessageBox(L"密码必须由数字、字母、下划线组成,长度至少为六位");
GetDlgItem(IDC_EPSW)->SetFocus();
}
if (regex_match(str_pwd, re_2) && regex_match(str_name, re_1))
{
//用户名及密码符合要求
}