Parse phone fields

Here is a function I use to parse phone fields. I've tested this on Windows 95/NT with Visual C++ 5.0 SP1/2.

This works by changing the value to a formatted phone number.

I'm sure there could be some code improvements here, and would appreciate any suggestions anyone might have.

void AFXAPI DDX_Phone(CDataExchange* pDX, int nIDC, CString& value);

void AFXAPI DDX_Phone(CDataExchange* pDX, int nIDC, CString& value)
      HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
      if (pDX->m_bSaveAndValidate)
            int nLen = ::GetWindowTextLength(hWndCtrl);
            ::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen),nLen+1);

            CString newValue;
            for(int x=0; x < value.GetLength();x++)
                  if(value[x] != '(' &&
                        value[x] != ')' &&
                        value[x] != '-' &&
                        value[x] != ' ' &&
                        value[x] != '.')

                        newValue += value[x];

            else if(newValue.GetLength()==10)
            else if(newValue.GetLength()>10)
                newValue.Format("(%s) %s-%s%s",newValue.Left(3),newValue.Mid(3,3),newValue.Mid(6,4),newValue.Right(newValue.GetLength()-10));
                newValue = value;

            AfxSetWindowText(hWndCtrl, newValue);


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date