IT정보공유/C#

C# TextBox 전화번호 하이픈 자동완성 (전화번호 포맷 자동입력)

알지오™ 2020. 5. 8.

오늘은 Textbox에 전화번호를 입력할 때 자동으로 하이픈(-)을 삽입해 주는 샘플 코드입니다.
핵심은 전화번호 자릿수를 계산해서 "-" 문자를 넣어주는 것이고
이를 위해서 TextBox의 KeyPress 이벤트 핸들러와 KeyUp 이벤트 핸들러에서 처리를 해줍니다.

 

private void tePhone_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsDigit(e.KeyChar) || e.KeyChar == Convert.ToIntel32(Keys.Back)) // || (e.KeyChar == '-') 
    {
     
    }
    else
    {
        e.Handled = true;
    }

}

 

KeyPress 이벤트핸들러에서는 숫자키와 백스페이스 키만 입력할 수 있도록 제한을 걸었고, 그 외의 숫자는
키 이벤트를 무시하도록 e.Handled 를 true로 세팅합니다.

 

private void tePhone_KeyUp(object sender, KeyEventArgs e)
{
    tePhone.Text = autoHyphen(tePhone.Text);
    tePhone.SelectionStart = tePhone.Text.Length;
}

 

KeyUp 이벤트핸들러에서는 autoHyphen 함수를 호출해서 전화번호에 하이픈을 삽입한 결과를 가져온 후
tePhone 컨트롤의 포커스를 맨 끝으로 옮겨주는 역할을 합니다.


전화번호 하이픈 자동입력 샘플 소스코드의 핵심은 autoHyphen() 함수죠.
지역번호가 보통은 3자리인데, 서울은 2자리이기 때문에 지역번호인 "02"에 대해서 체크를 한번 더 해주고 있습니다.

 

public string autoHyphen(string tel)
{
    string tmpTel = tel.Replace("-", "");
    string tel1 = string.Empty;
	string tel2 = string.Empty;
	string tel3 = string.Empty;
	string tel_total = string.Empty;


    if(tmpTel.Length >= 2 && tmpTel.Length < 8)
    {
        if(tmpTel.Substring(0, 2) != "02")
        {
            if(tmpTel.Length == 3)
            {
                tel_total = tmpTel + "-";
            }
            else if(tmpTel.Length > 3 && tmpTel.Length < 6)
            {
                tel1 = tmpTel.Substring(0, 3);
                tel2 = tmpTel.Substring(3, tmpTel.Length-3);
                tel_total = tel1 + "-" + tel2;
            }
            else if (tmpTel.Length > 3 && tmpTel.Length > 6)
            {
                tel1 = tmpTel.Substring(0, 3);
                tel2 = tmpTel.Substring(3, 3);
                tel3 = tmpTel.Substring(6, tmpTel.Length - 6);
                tel_total = tel1 + "-" + tel2 + "-" + tel3;
            }
            else
            {
                tel_total = tel;
            }
        }
        else
        {
            if (tmpTel.Length == 2)
            {
                tel_total = tmpTel + "-";
            }
            else if (tmpTel.Length > 2 && tmpTel.Length < 6)
            {
                tel1 = tmpTel.Substring(0, 2);
                tel2 = tmpTel.Substring(2, tmpTel.Length - 2);
                tel_total = tel1 + "-" + tel2;
            }
            else if (tmpTel.Length > 2 && tmpTel.Length > 5)
            {
                tel1 = tmpTel.Substring(0, 2);
                tel2 = tmpTel.Substring(2, 3);
                tel3 = tmpTel.Substring(5, tmpTel.Length - 5);
                tel_total = tel1 + "-" + tel2 + "-" + tel3;
            }


        }
    }
    else if (tmpTel.Length == 8 && tmpTel.Substring(0, 2) == "02")
    {
        tel1 = tmpTel.Substring(0, 2);
        tel2 = tmpTel.Substring(2, 3);
        tel3 = tmpTel.Substring(3, 3);
        tel_total = tel1 + "-" + tel2 + "-" + tel3;
    }
    else if (tmpTel.Length == 8 && tmpTel.Substring(0, 2) != "02")
    {
        tel1 = tmpTel.Substring(0, 4);
        tel2 = tmpTel.Substring(4, 4);
        tel_total = tel1 + "-" + tel2;
    }
    else if (tmpTel.Length == 9 && tmpTel.Substring(0, 2) == "02")
    {
        tel1 = tmpTel.Substring(0, 2);
        tel2 = tmpTel.Substring(2, 3);
        tel3 = tmpTel.Substring(5, 4);
        tel_total = tel1 + "-" + tel2 + "-" + tel3;
    }
    else if (tmpTel.Length == 9 && tmpTel.Substring(0, 2) != "02")    
    {
        tel1 = tmpTel.Substring(0, 3);
        tel2 = tmpTel.Substring(3, 4);
        tel3 = tmpTel.Substring(7, 2);
        tel_total = tel1 + "-" + tel2 + "-" + tel3;
    }
    else if (tmpTel.Length == 10 && tmpTel.Substring(0, 2) == "02")    
    {
        tel1 = tmpTel.Substring(0, 2);
        tel2 = tmpTel.Substring(2, 4);
        tel3 = tmpTel.Substring(6, 4);
        tel_total = tel1 + "-" + tel2 + "-" + tel3;
    }
    else if (tmpTel.Length == 10 && tmpTel.Substring(0, 2) != "02")    
    {
        tel1 = tmpTel.Substring(0, 3);
        tel2 = tmpTel.Substring(3, 3);
        tel3 = tmpTel.Substring(6, 4);
        tel_total = tel1 + "-" + tel2 + "-" + tel3;
    }
    else if (tmpTel.Length == 11)    
    {
        tel1 = tmpTel.Substring(0, 3);
        tel2 = tmpTel.Substring(3, 4);
        tel3 = tmpTel.Substring(7, 4);
        tel_total = tel1 + "-" + tel2 + "-" + tel3;
    }
    else
    {
        tel_total = tmpTel;
    }
    return tel_total;           

}

 

보통 전화번호에 하이픈을 자동입력해 주는 샘플 코드를 찾아보면 최종입력된 전화번호의 자릴수를 이용해
하이픈을 전화번호 형식에 맞게 삽입해 주는데, 
이렇게 키 이벤트 핸들러를 통해 계속 전화번호를 실시간으로 수정해 주도록하는게 아무래도 보기가 좋습니다.

댓글

💲 추천 글