Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -839,3 +840,55 @@ private val firaSansFamily = FontFamily()

val LightBlue = Color(0xFF0066FF)
val Purple = Color(0xFF800080)

// [START android_compose_text_auto_format_phone_number_validatetext]
@Composable
fun ValidateInput() {
class EmailViewModel : ViewModel() {
var email by mutableStateOf("")
private set

val emailHasErrors by derivedStateOf {
if (email.isNotEmpty()) {
// Email is considered erroneous until it completely matches EMAIL_ADDRESS.
!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
} else {
false
}
}

fun updateEmail(input: String) {
email = input
}
}

@Composable
fun ValidatingInputTextField(
email: String,
updateState: (String) -> Unit,
validatorHasErrors: Boolean
) {
val emailViewModel = EmailViewModel()
OutlinedTextField(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp),
value = email,
onValueChange = updateState,
label = { Text("Email") },
isError = validatorHasErrors,
supportingText = {
if (validatorHasErrors) {
Text("Incorrect email format.")
}
}
)

ValidatingInputTextField(
email = emailViewModel.email,
updateState = { input -> emailViewModel.updateEmail(input) },
validatorHasErrors = emailViewModel.emailHasErrors
)
}
}
// [END android_compose_text_auto_format_phone_number_validatetext]