r18ul
r18ul

Reputation: 1064

Validation for entered string in Edit Box in MFC

I have created a user login Dialog Box in MFC, which have two edit fields, for username & password respectively. I need to restrict/disable user from typing "space" & a few "special characters" in Login/Password Edit Box fields. Please help me with this. Thank you.

EDIT: I'm validating the Username & Password with my SQLite database. Everything is working fine. Additional requirement is to restrict user from typing spaces in the edit field. Please explain with some simple example. Thank you.

Upvotes: 1

Views: 8093

Answers (4)

Ritesh Gohil
Ritesh Gohil

Reputation: 1

As per above answer, this works fine but you can also override CEdit with ASCII values,(in this case we used HEX values followed by '\x')

BOOL TestDlg::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message==WM_CHAR)
    {
       if( ( pMsg->wParam >= '\x20'  &&   pMsg->wParam <= '\x2D'))
       {
            return true;
       }
    }
    return CEdit::PreTranslateMessage(pMsg);
}

Upvotes: 0

Jeeva
Jeeva

Reputation: 4663

You need to subclass(Inherit) the CEdit control of MFC and override PreTranslateMessage and handle WM_CHAR message and filter the characters there

BOOL CMyEditBox::PreTranslateMessage(MSG* pMsg)
{
    int  nTextLength = this->GetWindowTextLength();
    if(pMsg->message==WM_CHAR)
    {
       // Ignoring 0 to 9
       if( ( pMsg->wParam >= '0' &&   pMsg->wParam <= '9' ) )
       {
            return true;
       }
    }
    return CEdit::PreTranslateMessage(pMsg);
}

Upvotes: 4

MikMik
MikMik

Reputation: 3466

Check http://www.flounder.com/validating_edit_control.htm. It has an explanation plus sample code

EDIT
By the way, I'm not sure that "live validation" for a password field is a good idea. I think "lazy validation" is a better solution here.

Upvotes: 1

Ajay
Ajay

Reputation: 18411

Handle the edit-control change in EN_CHANGE notification message.

Upvotes: 1

Related Questions