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

  • 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.

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

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