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.

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

//Implementation
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);
            value.ReleaseBuffer();

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

                        newValue += value[x];

            }
            if(newValue.GetLength()==7)
				newValue.Format("%s-%s",newValue.Left(3),newValue.Right(4));
            else if(newValue.GetLength()==10)
                newValue.Format("(%s)%s-%s",newValue.Left(3),newValue.Mid(3,3),newValue.Right(4));
            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));
            else
                newValue = value;

            AfxSetWindowText(hWndCtrl, newValue);
      }
}



Comments

  • 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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds