Solved

How to verify why user input?

Posted on 2001-06-05
11
227 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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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
Comment Utility
Using isdigit(int c). isdigit returns a non-zero value if c is a decimal digit (0 ? 9).
0
 

Author Comment

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

Expert Comment

by:migel
Comment Utility
hi!
look at www.codeguru.com editcontrol section for mask edit articles
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

0
 

Expert Comment

by:alortiz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi GloriousRain,

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

6 Experts available now in Live!

Get 1:1 Help Now