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

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds