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

  • All businesses have a core set of applications that are critical to successful growth. These applications require a higher level of availability than other applications and services in the organization. There is a trade-off, however, to increasing application availability through traditional high-availability clustering. Businesses can see costs surge in terms of additional hardware and clustering software/support, as well as additional costs and complexities due to increased operational management …

  • Live Event Date: July 30, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this upcoming eSeminar that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds