Skip to content

Commit

Permalink
FEAT: 이메일, 전화번호 형식 규약 추가 (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Feb 7, 2024
1 parent 7afbee7 commit 2b8ca85
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class ProfileEditViewModel @Inject constructor(
private val _email = MutableStateFlow("")
val email: StateFlow<String> = _email

private val _emailError = MutableStateFlow<String?>(null)
val emailError: StateFlow<String?> = _emailError

private val _phoneNumError = MutableStateFlow<String?>(null)
val phoneNumError: StateFlow<String?> = _phoneNumError


private val _selectedImage = MutableStateFlow<Uri?>(null)
val selectedImage: StateFlow<Uri?> = _selectedImage.asStateFlow()

Expand Down Expand Up @@ -74,11 +81,21 @@ class ProfileEditViewModel @Inject constructor(

fun updateEmail(newEmail: String) {
_email.value = newEmail
if (!isValidEmail(newEmail)) {
_emailError.value = "유효한 이메일 주소를 입력해주세요"
} else {
_emailError.value = null
}
validateFields()
}

fun updatePhoneNumber(newPhoneNumber: String) {
_phoneNum.value = newPhoneNumber
if (!isValidPhoneNumber(newPhoneNumber)) {
_phoneNumError.value = "유효한 전화번호를 입력해주세요 (010-1234-1234)"
} else {
_phoneNumError.value = null
}
validateFields()
}

Expand All @@ -89,4 +106,15 @@ class ProfileEditViewModel @Inject constructor(
_email.value.isNotBlank() &&
_phoneNum.value.isNotBlank()
}

private fun isValidEmail(email: String): Boolean {
val emailRegex = Regex("^\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,3}\$")
return email.matches(emailRegex)
}

private fun isValidPhoneNumber(phoneNumber: String): Boolean {
val phoneRegex = Regex("^01(?:0|1|[6-9])-(?:\\d{3}|\\d{4})-\\d{4}\$")
return phoneNumber.matches(phoneRegex)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ fun ProfileEditScreen(
} else {
stringResource(id = R.string.signin_member_hint1_3)
}
val emailError by viewModel.emailError.collectAsState()
val phoneNumError by viewModel.phoneNumError.collectAsState()

var isOpenPartBottomSheet by remember { mutableStateOf(false) }
var isOpenLikeCategoryBottomSheet by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -240,6 +242,13 @@ fun ProfileEditScreen(
value = email,
onValueChange = viewModel::updateEmail
)
emailError?.let { error ->
Text(
text = error,
style = KusitmsTypo.current.Caption1,
color = KusitmsColorPalette.current.Grey400
)
}

//전화번호
Spacer(modifier = Modifier.height(20.dp))
Expand All @@ -254,6 +263,14 @@ fun ProfileEditScreen(
value = phoneNum,
onValueChange = viewModel::updatePhoneNumber
)
phoneNumError?.let { error ->
Spacer(modifier = Modifier.height(4.dp))
Text(
text = error,
style = KusitmsTypo.current.Caption1,
color = KusitmsColorPalette.current.Grey400
)
}
}
}
}
Expand Down

0 comments on commit 2b8ca85

Please sign in to comment.