관리 메뉴

웹개발 블로그

JS, JAVA(전화번호 하이픈) 본문

◆JAVA/메서드

JS, JAVA(전화번호 하이픈)

쿠키린 2024. 3. 14. 13:20

자바스크립트

function formatPhoneNumber(value){
    //
    // 서울 (02): 7자리
    // 부산 (051): 7자리
    // 대구 (053): 7자리
    // 인천 (032): 7자리
    // 광주 (062): 7자리
    // 대전 (042): 7자리
    // 울산 (052): 7자리
    // 세종 (044): 7자리
    // 경기도 지역번호 (031, 032, 033): 7자리
    // 강원도 지역번호 (033, 043, 044): 7자리
    // 충청북도 (043): 7자리
    // 충청남도 (041, 043): 7자리
    // 전라북도 (063): 7자리
    // 전라남도 (061, 064): 7자리
    // 경상북도 (054, 055): 7자리
    // 경상남도 (052, 055): 7자리
    // 제주도 (064): 7자리


    let formattedValue  = value;
    let len = value.length;
    if (len > 10) {
        formattedValue = value.replace(/(\d{3})(\d{4})(\d{4})/, '$1-$2-$3');
    }else if(len == 8){
        formattedValue = value.replace(/(\d{4})(\d{4})/, '$1-$2');
    }else{
        //지역번호
        if (value.startsWith("02")) {//서울
            if(len == 10){
                formattedValue = value.replace(/(\d{2})(\d{4})(\d{4})/, '$1-$2-$3');
            }else{
                formattedValue = value.replace(/(\d{2})(\d{3})(\d{4})/, '$1-$2-$3');
            }
        }else if (value.startsWith("031") || value.startsWith("032") || value.startsWith("033")
            || value.startsWith("041") || value.startsWith("042") || value.startsWith("043")
            || value.startsWith("051") || value.startsWith("052") || value.startsWith("053")
            || value.startsWith("054") || value.startsWith("055") || value.startsWith("061")
            || value.startsWith("062") || value.startsWith("063") || value.startsWith("064")
        ) {
            //그외 지역
            formattedValue = value.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
        }else if(len == 10){
            formattedValue = value.replace(/(\d{2})(\d{4})(\d{4})/, '$1-$2-$3');
        }
    }
    return formattedValue;
}

 

 

자바

private String formatPhoneNumber(String value){
   String formattedValue = "";
   int len = value.length();
   if (len > 10) {
      formattedValue = value.replaceFirst("(\\d{3})(\\d{4})(\\d{4})", "$1-$2-$3");
   } else if (len == 8) {
      formattedValue = value.replaceFirst("(\\d{4})(\\d{4})", "$1-$2");
   } else {
      // 지역번호
      if (value.startsWith("02")) { // 서울
         if (len == 10) {
            formattedValue = value.replaceFirst("(\\d{2})(\\d{4})(\\d{4})", "$1-$2-$3");
         } else {
            formattedValue = value.replaceFirst("(\\d{2})(\\d{3})(\\d{4})", "$1-$2-$3");
         }
      }else if (value.startsWith("031") || value.startsWith("032") || value.startsWith("033")
            || value.startsWith("041") || value.startsWith("042") || value.startsWith("043")
            || value.startsWith("051") || value.startsWith("052") || value.startsWith("053")
            || value.startsWith("054") || value.startsWith("055") || value.startsWith("061")
            || value.startsWith("062") || value.startsWith("063") || value.startsWith("064")
      ) {
         // 그외 지역
         formattedValue = value.replaceFirst("(\\d{3})(\\d{3})(\\d{4})", "$1-$2-$3");
      } else if (len == 10) {
         formattedValue = value.replaceFirst("(\\d{2})(\\d{4})(\\d{4})", "$1-$2-$3");
      }
   }

   return formattedValue;
}