improvement: make phone number input more flexible with auto-formatting
TaxBaik CI/CD / build-and-deploy (push) Successful in 1m37s
TaxBaik CI/CD / build-and-deploy (push) Successful in 1m37s
- Accept phone numbers with or without hyphens/spaces (01012345678, 010-1234-5678, etc) - Auto-format to standard 010-XXXX-XXXX or 010-XXXX-XXXXX format on backend - Remove strict regex validation that forced user input format - Better UX: accept user input as-is and normalize in backend Closes issue with phone number validation being too strict. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,7 @@ public class InquiryService(
|
||||
IInquiryNotificationService notificationService,
|
||||
IMemoryCache memoryCache)
|
||||
{
|
||||
private static readonly Regex PhoneRegex = new(@"^01[0-9]-\d{3,4}-\d{4}$");
|
||||
private static readonly Regex PhoneRegex = new(@"^01[0-9]\d{7,8}$");
|
||||
|
||||
public async Task<int> SubmitAsync(
|
||||
string name, string phone, string serviceType, string message,
|
||||
@@ -21,8 +21,11 @@ public class InquiryService(
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ValidationException("이름을 입력하세요.");
|
||||
|
||||
if (!PhoneRegex.IsMatch(phone))
|
||||
throw new ValidationException("올바른 전화번호를 입력하세요. (예: 010-1234-5678)");
|
||||
var cleanPhone = phone?.Replace("-", "").Replace(" ", "").Trim() ?? "";
|
||||
if (!PhoneRegex.IsMatch(cleanPhone) || cleanPhone.Length < 10)
|
||||
throw new ValidationException("올바른 전화번호를 입력하세요. (예: 01012345678 또는 010-1234-5678)");
|
||||
|
||||
var formattedPhone = FormatPhoneNumber(cleanPhone);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
throw new ValidationException("문의 내용을 입력하세요.");
|
||||
@@ -30,7 +33,7 @@ public class InquiryService(
|
||||
var inquiry = new Inquiry
|
||||
{
|
||||
Name = name.Trim(),
|
||||
Phone = phone.Trim(),
|
||||
Phone = formattedPhone,
|
||||
Email = string.IsNullOrWhiteSpace(email) ? null : email.Trim(),
|
||||
ServiceType = serviceType ?? "기타",
|
||||
Message = message.Trim(),
|
||||
@@ -82,8 +85,9 @@ public class InquiryService(
|
||||
if (string.IsNullOrWhiteSpace(dto.Name))
|
||||
throw new ValidationException("이름을 입력하세요.");
|
||||
|
||||
if (!PhoneRegex.IsMatch(dto.Phone))
|
||||
throw new ValidationException("올바른 전화번호를 입력하세요. (예: 010-1234-5678)");
|
||||
var cleanPhone = dto.Phone?.Replace("-", "").Replace(" ", "").Trim() ?? "";
|
||||
if (!PhoneRegex.IsMatch(cleanPhone) || cleanPhone.Length < 10)
|
||||
throw new ValidationException("올바른 전화번호를 입력하세요. (예: 01012345678 또는 010-1234-5678)");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(dto.Message))
|
||||
throw new ValidationException("문의 내용을 입력하세요.");
|
||||
@@ -92,7 +96,7 @@ public class InquiryService(
|
||||
throw new ValidationException("지원하지 않는 문의 상태입니다.");
|
||||
|
||||
inquiry.Name = dto.Name.Trim();
|
||||
inquiry.Phone = dto.Phone.Trim();
|
||||
inquiry.Phone = FormatPhoneNumber(cleanPhone);
|
||||
inquiry.Email = string.IsNullOrWhiteSpace(dto.Email) ? null : dto.Email.Trim();
|
||||
inquiry.ServiceType = string.IsNullOrWhiteSpace(dto.ServiceType) ? "기타" : dto.ServiceType.Trim();
|
||||
inquiry.Message = dto.Message.Trim();
|
||||
@@ -144,6 +148,23 @@ public class InquiryService(
|
||||
|
||||
return InquiryStatusMapper.ToStorageValue(parsed);
|
||||
}
|
||||
|
||||
private static string FormatPhoneNumber(string phone)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(phone))
|
||||
return phone ?? "";
|
||||
|
||||
var clean = phone.Replace("-", "").Replace(" ", "").Trim();
|
||||
if (clean.Length < 10 || clean.Length > 11)
|
||||
return clean;
|
||||
|
||||
return clean.Length switch
|
||||
{
|
||||
10 => $"{clean[..3]}-{clean[3..6]}-{clean[6..]}", // 01012345678 → 010-123-45678
|
||||
11 => $"{clean[..3]}-{clean[3..7]}-{clean[7..]}", // 010123456789 → 010-1234-56789
|
||||
_ => clean
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class ValidationException : Exception
|
||||
|
||||
Reference in New Issue
Block a user