Click here to Skip to main content
12,292,006 members (76,953 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked

Use wildcard to compare string and validate input

, 3 Feb 2005
Rate this:
Please Sign up or sign in to vote.
This article describes how to use wildcard to make input validation in CEdit control or to compare string with wildcard.


This article was inspired by Jack Handy (Wildcard string compare).

It describes a function to compare strings with wildcards. I have modified Jack's function to use MFC and use optional character in the wildcard string.

Wildcard sample

If you want to check a string like 'test' or 'tet', you may use the wildcard like this : te^st where ^ indicates an optional char.

I included another parameter to limit characters used in wildcard * or ?.

if (WildMatch("^-*?^.*", sValue, "0123456789")) {
    MessageBox("OK you have a real");
} else {
    MessageBox("Not a real");

This sample validates the string in sValue has a good format to be a real number.

Function code

BOOL WildMatch(CString sWild, CString sString, CString sLimitChar)
    BOOL bAny = FALSE;
    BOOL bNextIsOptional = FALSE;
    BOOL bAutorizedChar = TRUE;

    int i=0;
    int j=0;

    // Check all the string char by char
    while (i<sString.GetLength()) 
      // Check index for array overflow
      if (j<sWild.GetLength())
          // Manage '*' in the wildcard
          if (sWild[j]=='*') 
          // Go to next character in the wildcard

          // Enf of the string and wildcard end 
          // with *, only test string validity
          if (j>=sWild.GetLength()) 
          // Check end of the string
          while (!sLimitChar.IsEmpty() && i<sString.GetLength()) 
          // If this char is not ok, return false
          if (sLimitChar.Find(sString[i])<0)
              return FALSE;


          return TRUE;

          bAny = TRUE;
          bNextIsOptional = FALSE;
              // Optional char in the wildcard
              if (sWild[j]=='^')
              // Go to next char in the wildcard and indicate 
              // that the next is optional
              bNextIsOptional = TRUE;
                bAutorizedChar = 
                  ((sLimitChar.IsEmpty()) || (sLimitChar.Find(sString[i])>=0));

                // IF :
                  if (// Current char match the wildcard
                    sWild[j] == sString[i] 
                    // '?' is used and current char is in autorized char list
                    || (sWild[j] == '?' && bAutorizedChar)
                    // Char is optional and it's not in the string
                    // and it's necessary to test if '*' make any 
                    // char browsing
                    || (bNextIsOptional && !(bAny && bAutorizedChar))) 
                    // If current char match wildcard, 
                    // we stop for any char browsing
                    if (sWild[j] == sString[i])
                        bAny = FALSE;

                    // If it's not an optional char who is not present,
                    // go to next
                    if (sWild[j] == sString[i] || sWild[j] == '?')


                    bNextIsOptional = FALSE;
                    // If we are in any char browsing ('*') 
                    // and curent char is autorized
                    if (bAny && bAutorizedChar)
                        // Go to next
                        return FALSE;
        // End of the wildcard but not the 
        // end of the string => 
        // not matching
        return FALSE;

    if (j<sWild.GetLength() && sWild[j]=='^')
        bNextIsOptional = TRUE;

    // If the string is shorter than wildcard 
    // we test end of the 
    // wildcard to check matching
    while ((j<sWild.GetLength() && sWild[j]=='*') || bNextIsOptional)
        bNextIsOptional = FALSE;

        if (j<sWild.GetLength() && sWild[j]=='^')
            bNextIsOptional = TRUE;

    return j>=sWild.GetLength();

CEdit input validation

This sample shows you how to use this function to control input validation in a dynamic way.

Be careful when you define your wildcard, because it must be always true! If you want more advanced testing, don't test in real time but do only when the user validates.

For example, if you use ^-?*^.*, the user can never put the minus sign first, because '?' specifies one char is always necessary!

You can put this code in the OnChar of your CEdit child class, or if you don't want to derive a new class, you can put it in the PreTranslateMessage of the mother window, like:

if (pMsg->message==WM_CHAR && pMsg->hwnd==pEdit->m_hWnd && 
    // Define parameters
    CString sSaveText="";
    CString sNewText="";
    CString sLimitChar="0123456789";
    CString sWildCard="^-*^.*";
    int iNbMaxChar = 10;

        // If we use only upper case, makeupper
    if (m_bMakeUpper)
        pMsg->wParam = toupper(pMsg->wParam);

    DWORD dwSaveSel = pEdit->GetSel();


        // Transmit this char to the CEdit to process it
    pEdit->SendMessage(WM_CHAR, pMsg->wParam, pMsg->lParam);


        // Check if new text is ok
    if (!WildMatch(m_sWildCard, sNewText, sLimitChar) || 
                // if not, rollback


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

DarkYoda M
Web Developer
France France
I'm a French Industrial Developer,

I like codeproject! It help me to solve my problems many times...

I'm interresting in electronic an embedded module like snap (, but I don't have any time to go hard in this way Wink | ;)

You may also be interested in...

Comments and Discussions

Generalsome combinations dont match. Pin
Nic Wilson2-Feb-09 10:11
memberNic Wilson2-Feb-09 10:11 
GeneralRe: some combinations dont match. Pin
Joey Rogers12-Oct-10 14:47
memberJoey Rogers12-Oct-10 14:47 
I also found that this function did now work. Patterns like this would fail:

"*source location*" compared with "existing source location - second"

The first "s" in the string would throw it off.

I found another function that appears to work correctly:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 3 Feb 2005
Article Copyright 2005 by DarkYoda M
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid