dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Password style for the text control

  1. #1
    Join Date
    Aug 2002
    Posts
    687

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,843

    Re: Password style for the text control

    Where is TE_PASSWORD from? Are you using an edit box and meant to type ES_PASSWORD?

  3. #3
    Join Date
    Aug 2002
    Posts
    687

    Re: Password style for the text control

    Hi,
    Quote Originally Posted by Arjay View Post
    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.

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,203

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,203

    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
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,203

    Re: Password style for the text control

    Quote Originally Posted by OneEyeMan View Post
    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.
    1. derive your own class from CEdit;
    2. add a member variable for keeping the password and a public method to get its value;
    3. handle WM_CHAR message and do not call the base class method CEdit::OnChar;
    4. in the WM_CHAR message handler, append the password string;
    5. (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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)