-
November 15th, 2018, 06:45 PM
#1
Password style for the text control
Hi, ALL,
It is possible to create a password-style text control with using TE_PASSWORD. However, someone can just guess the length of the password by how many letters are typed.
Is it possible to create a password field where the typed thing won't be echoed?
Qt does have such style, but what about native Windows control?
Thank you.
-
November 15th, 2018, 08:46 PM
#2
Re: Password style for the text control
Where is TE_PASSWORD from? Are you using an edit box and meant to type ES_PASSWORD?
-
November 15th, 2018, 09:18 PM
#3
Re: Password style for the text control
Hi,
Originally Posted by Arjay
Where is TE_PASSWORD from? Are you using an edit box and meant to type ES_PASSWORD?
Yes, sorry, it is ES_PASSWORD.
Thank you.
-
November 17th, 2018, 03:02 AM
#4
Re: Password style for the text control
"Blind" typing of password is used in some console aplications. If have a GUI, that may be frustrating for the user.
Instead, you can let the default behavior of the edit control with ES_PASSWORD style and validate the pasword for strenght, i.e. to have at least 8 characters, small letters, capital letters, digits and signs.
This way, there is no more reason to worry in case someone knows the password length.
-
November 17th, 2018, 05:59 AM
#5
Re: Password style for the text control
Here is an example that validates the password when the user pushes "OK" button:
Code:
void CSignInDlg::OnOK()
{
UpdateData();
CPasswordChecker password(m_strPassword);
if (!password.IsValid()) // validate password
{
// prompt user for invalid password
AfxMessageBox(IDS_INVALID_PASSWORD);
// put focus back in password field and select contents
CEdit* pEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDIT_PASSWORD));
ASSERT_VALID(pEdit);
pEdit->SetFocus();
pEdit->SetSel(0, m_strPassword.GetLength());
return;
}
CDialogEx::OnOK();
}
And here is the CPasswordChecker class implementation:
Code:
CPasswordChecker::CPasswordChecker(const CString& strPassword, UINT nMinLength /*= 8*/)
: m_strPassword(strPassword),
m_nMinLength(nMinLength)
{
}
bool CPasswordChecker::IsValid()
{
const int nLength = m_strPassword.GetLength();
if (static_cast<UINT>(nLength) < m_nMinLength)
return false;
bool bHasLower = false, bHasUpper = false;
bool bHasDigit = false, bHasPunct = false;
for (int nIndex = 0; nIndex < nLength; nIndex++)
{
TCHAR ch = m_strPassword[nIndex];
if (_istlower(ch))
bHasLower = true;
else if (_istupper(ch))
bHasUpper = true;
else if (_istdigit(ch))
bHasDigit = true;
else if (_istpunct(ch))
bHasPunct = true;
}
return bHasLower && bHasUpper && bHasDigit && bHasPunct;
}
You can find the complete code in the Demo application, attached here.
Demo_password_checker.zip
-
November 17th, 2018, 08:33 AM
#6
Re: Password style for the text control
Originally Posted by OneEyeMan
Is it possible to create a password field where the typed thing won't be echoed?
If you really, really want to make an edit control with "no echo" it's not so much to sweat.
- derive your own class from CEdit;
- add a member variable for keeping the password and a public method to get its value;
- handle WM_CHAR message and do not call the base class method CEdit::OnChar;
- in the WM_CHAR message handler, append the password string;
- (optional) empty the password in the handler of EN_SETFOCUS reflected notification.
Here is a code example:
Code:
class CBlindEdit : public CEdit
{
// Data
CString m_strPassword;
// Operations
public:
CString GetPassword() { return m_strPassword; }
// Message handlers
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnEnSetfocus();
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
};
Code:
BEGIN_MESSAGE_MAP(CBlindEdit, CEdit)
ON_WM_CHAR()
ON_CONTROL_REFLECT(EN_SETFOCUS, &CBlindEdit::OnEnSetfocus)
END_MESSAGE_MAP()
void CBlindEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// do not call CEdit::OnChar!
CString strChar;
strChar.Format(_T("%c"), nChar);
m_strPassword += strChar;
}
void CBlindEdit::OnEnSetfocus()
{
m_strPassword.Empty();
}
[ Later edit ]
Note again that such of "edit with no echo" may be confusing for a GUI user, as already said in my first answer. So, prefer a "standard" edit control with ES_PASSWORD style set and eventually, validate the password strength.
Last edited by ovidiucucu; November 18th, 2018 at 02:35 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|