feat: implement proper Korean phone number validation and formatting
TaxBaik CI/CD / build-and-deploy (push) Successful in 3m23s
TaxBaik CI/CD / build-and-deploy (push) Successful in 3m23s
- Add comprehensive Korean phone number regex validation - Support all area codes: 02, 031-064, 070, 0505-0509 - Support all mobile carriers: 010-019 - Intelligent formatting based on area code (2-3 digits) - Client-side JavaScript: real-time formatting + validation - Backend C#: robust validation + formatting for storage Handles all Korean phone number formats: - Landline: 02-123-4567, 031-1234-5678 - Mobile: 010-1234-5678 - VoIP: 070-1234-5678, 0505-1234-5678 Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,11 @@ public class InquiryService(
|
||||
IInquiryNotificationService notificationService,
|
||||
IMemoryCache memoryCache)
|
||||
{
|
||||
private static readonly Regex PhoneRegex = new(@"^\d{10,11}$");
|
||||
// 한국 전화번호 정규식
|
||||
// 휴대폰: 010~019, 070, 0505~0509
|
||||
// 고정전화: 02, 031~064
|
||||
private static readonly Regex PhoneRegex = new(
|
||||
@"^(0(?:2|3[1-3]|4[1-4]|5[1-5]|6[1-4]|70|50[5-9]|[7-9](?:\d{1,2})?)\d{7,8}|0\d{9,10})$");
|
||||
|
||||
public async Task<int> SubmitAsync(
|
||||
string name, string phone, string serviceType, string message,
|
||||
@@ -155,15 +159,59 @@ public class InquiryService(
|
||||
return phone ?? "";
|
||||
|
||||
var clean = phone.Replace("-", "").Replace(" ", "").Trim();
|
||||
if (clean.Length < 10 || clean.Length > 11)
|
||||
if (clean.Length < 10 || clean.Length > 11 || !clean.StartsWith("0"))
|
||||
return clean;
|
||||
|
||||
return clean.Length switch
|
||||
// 국번 추출 (2-3자리)
|
||||
var areaCode = GetAreaCode(clean);
|
||||
if (string.IsNullOrEmpty(areaCode))
|
||||
return clean;
|
||||
|
||||
var restLength = clean.Length - areaCode.Length;
|
||||
var restPart = clean.Substring(areaCode.Length);
|
||||
|
||||
// 나머지 부분을 3-4 또는 4-4로 분할
|
||||
if (restLength == 7)
|
||||
return $"{areaCode}-{restPart[..3]}-{restPart[3..]}"; // XXX-XXX-XXXX
|
||||
else if (restLength == 8)
|
||||
return $"{areaCode}-{restPart[..4]}-{restPart[4..]}"; // XXX-XXXX-XXXX
|
||||
else
|
||||
return clean;
|
||||
}
|
||||
|
||||
private static string GetAreaCode(string phone)
|
||||
{
|
||||
if (phone.Length < 10 || !phone.StartsWith("0"))
|
||||
return "";
|
||||
|
||||
// 3자리 국번: 070, 0505~0509
|
||||
if ((phone.StartsWith("070") ||
|
||||
(phone.StartsWith("050") && phone.Length > 2 && phone[3] >= '5' && phone[3] <= '9')))
|
||||
return phone[..3];
|
||||
|
||||
// 3자리 국번: 031~064
|
||||
if (phone.Length >= 3 &&
|
||||
phone[0] == '0' &&
|
||||
char.IsDigit(phone[1]) && char.IsDigit(phone[2]))
|
||||
{
|
||||
10 => $"{clean[..4]}-{clean[4..7]}-{clean[7..]}", // 0089702448 → 0089-702-2448
|
||||
11 => $"{clean[..3]}-{clean[3..7]}-{clean[7..]}", // 01012345678 → 010-1234-5678
|
||||
_ => clean
|
||||
};
|
||||
var areaPrefix = int.Parse(phone.Substring(1, 2));
|
||||
if ((areaPrefix >= 31 && areaPrefix <= 39) || // 경기, 인천
|
||||
(areaPrefix >= 41 && areaPrefix <= 48) || // 충청
|
||||
(areaPrefix >= 51 && areaPrefix <= 54) || // 부산, 울산
|
||||
(areaPrefix >= 61 && areaPrefix <= 64) || // 전라
|
||||
(areaPrefix >= 71 && areaPrefix <= 74)) // 경상
|
||||
return phone[..3];
|
||||
}
|
||||
|
||||
// 2자리 국번: 02
|
||||
if (phone.StartsWith("02"))
|
||||
return "02";
|
||||
|
||||
// 기타 휴대폰 (010~019)
|
||||
if (phone[0] == '0' && phone[1] == '1' && phone.Length >= 11)
|
||||
return phone[..3];
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user