Solved

How to verify why user input?

Posted on 2001-06-05
11
233 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
viewing source code from eclipse 13 106
move a line in eclipse 3 102
JQuery serialize and unserialize 8 84
Capture logon name 13 73
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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