65.9K
CodeProject is changing. Read more.
Home

Use wildcard to compare string and validate input

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.89/5 (8 votes)

Feb 3, 2005

1 min read

viewsIcon

59039

This article describes how to use wildcard to make input validation in CEdit control or to compare string with wildcard.

Introduction

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
          j++;

          // 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;

          i++;
          }

          return TRUE;
          }

          bAny = TRUE;
          bNextIsOptional = FALSE;
          } 
          else 
          {
              // Optional char in the wildcard
              if (sWild[j]=='^')
              {
              // Go to next char in the wildcard and indicate 
              // that the next is optional
              j++;
 
              bNextIsOptional = TRUE;
              }
              else
              {
                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] == '?')
                        i++;

                    j++;

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

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


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

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

    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 && 
         pMsg->wParam>=32)
{
    // 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();

    pEdit->GetWindowText(sSaveText);

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

    pEdit->GetWindowText(sNewText);

        // Check if new text is ok
    if (!WildMatch(m_sWildCard, sNewText, sLimitChar) || 
          sNewText.GetLength()>iNbMaxChar)
    {
                // if not, rollback
        pEdit->SetWindowText(sSaveText);
        pEdit->SetSel(dwSaveSel);
    }
}