Solved

How to verify why user input?

Posted on 2001-06-05
11
230 Views
Last Modified: 2013-11-20
Is there any function to verify user input??

eg. User key in something in the edit box, after press a button, I want to check what user had type in, whether numeric numbers or characters.

What I need to do??
0
Comment
Question by:jason_nothing
11 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 6155397
Hi jason_nothing,

you can do  i.e.:

- add a EN_CHANGE notification message handler for the edit control to the parent window's class,
there parse the string and modify if needed as you want it to be...
- derive your own class from CEdit and override a WM_CHAR message handler to filter out
keys which shouldn't be allowed.


ZOPPO
0
 

Author Comment

by:jason_nothing
ID: 6155462
Hi ZOPPO,

I don't quite get what you mean, can you give an example in more details?? thanks
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6155463
Using isdigit(int c). isdigit returns a non-zero value if c is a decimal digit (0 ? 9).
0
 

Author Comment

by:jason_nothing
ID: 6155494
GloriosRain,
 
Can you show me more details on how to use the isdigit()??
0
 
LVL 12

Expert Comment

by:migel
ID: 6155539
hi!
look at www.codeguru.com editcontrol section for mask edit articles
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jason_nothing
ID: 6158237
It don't have any single function in MFC that can do this ?

0
 

Expert Comment

by:alortiz
ID: 6160839
Easy solution:

You need to create an event handler to do that checking.

If you are using the classwizard, create and map a member variable to the edit control, then select the event you want to catch (ie onclick for the button), and create a new functin for it. that fucntion will be evaluated whenever the even happens, from there you can check the data typed in the text box (by using the edit control propperties), etc.
However, if this functionality will be frecuently desired, you should derive a class from CEdit, to do pretty much the same as above, but using only the Edit control events

0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6162058
e.g of isdigit(int c)
...
CString strTest("a123");
for (int i = 0; i < strTest.GetLength(); i++)
   if (isdigit(strTest[i]))
   {
      CString s;
      s.Format("%c", strTest[i]);
      AfxMessageBox(s);
   }
...
you can use isalpha(int c) to check character.
isalpha returns a non-zero value if c is within the ranges A ? Z or a ? z.
0
 
LVL 2

Accepted Solution

by:
GloriousRain earned 120 total points
ID: 6162071
Hi jason_nothing,
Try this class, it's inherit from CEdit. It will checks character input with specific filter whenever user press a key in editbox. I have an example at bottom.

//MyEditCtrl.cpp
/////////////////////////////////////////////////////////////////////////////
// CMyEditCtrl

// bFilter = TRUE will filter out all chars in strFilter
// bFilter = FALSE will allow only those chars in strFilter
CMyEditCtrl::CMyEditCtrl(CString strFilter, BOOL bFilter)
     : m_bFilter(bFilter), m_strFilter(strFilter)
{
}

CMyEditCtrl::~CMyEditCtrl()
{
}

BEGIN_MESSAGE_MAP(CMyEditCtrl, CEdit)
     //{{AFX_MSG_MAP(CMyEditCtrl)
     ON_WM_CHAR()
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyEditCtrl message handlers

void CMyEditCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
     if (nChar > 32 && nChar < 127)
     {
          if ((m_bFilter && m_strFilter.Find(nChar) != -1) ||
             (!m_bFilter && m_strFilter.Find(nChar) == -1))
          {
               MessageBeep(0xFFFFFFFF);
               return;
          }
     }
     CEdit::OnChar(nChar, nRepCnt, nFlags);
}

//MyEditCtrl.h
class CMyEditCtrl : public CEdit
{
// Construction
public:
     CMyEditCtrl(CString strFilter, BOOL bFilter=FALSE);

// Attributes
private:
     CString m_strFilter;
     BOOL     m_bFilter;

// Operations
public:
     const CString&     GetFilterString() const;
               void     SetFilterString(const CString &strFilter);
               BOOL     GetFilter() const;
               void     SetFilter(BOOL bFilter);

// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CNtlEditCtrl)
     //}}AFX_VIRTUAL

// Implementation
public:
     virtual ~CMyEditCtrl();

     // Generated message map functions
protected:
     //{{AFX_MSG(CMyEditCtrl)
     afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
     //}}AFX_MSG

     DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

inline const CString& CMyEditCtrl::GetFilterString() const
{
     return m_strFilter;
}

inline void CMyEditCtrl::SetFilterString(const CString &strFilter)
{
     m_strFilter = strFilter;
}

inline BOOL CMyEditCtrl::GetFilter() const
{
     return m_bFilter;
}

inline void CMyEditCtrl::SetFilter(BOOL bFilter)
{
     m_bFilter = bFilter;
}

Here is example of above class:
CMyEditCtrl m_eIPAddress(_T("1234567890."));
0
 

Author Comment

by:jason_nothing
ID: 6162225
I think GloriousRain's answer was excellent!! very clear and details, so I have to reject your answer. Anyway, thanks for the comment.
0
 

Author Comment

by:jason_nothing
ID: 6162261
Hi GloriousRain,

    U are excellent, I can't did it without your help...thanks a lot!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now