@@ -38,6 +38,7 @@ import androidx.compose.material3.OutlinedTextField
3838import  androidx.compose.material3.Text 
3939import  androidx.compose.material3.TextField 
4040import  androidx.compose.runtime.Composable 
41+ import  androidx.compose.runtime.derivedStateOf 
4142import  androidx.compose.runtime.getValue 
4243import  androidx.compose.runtime.mutableStateOf 
4344import  androidx.compose.runtime.remember 
@@ -839,3 +840,56 @@ private val firaSansFamily = FontFamily()
839840
840841val  LightBlue  =  Color (0xFF0066FF )
841842val  Purple  =  Color (0xFF800080 )
843+ 
844+ //  [START android_compose_text_auto_format_phone_number_validatetext]
845+ @Composable
846+ fun  ValidateInput  () {
847+     class  EmailViewModel  : ViewModel () {
848+         var  email by mutableStateOf(" "  )
849+             private  set
850+ 
851+         val  emailHasErrors by derivedStateOf {
852+             if  (email.isNotEmpty()) {
853+                 //  Email is considered erroneous until it completely matches EMAIL_ADDRESS.
854+                 ! android.util.Patterns .EMAIL_ADDRESS .matcher(email).matches()
855+             } else  {
856+                 false 
857+             }
858+         }
859+ 
860+         fun  updateEmail (input :  String ) {
861+             email =  input
862+         }
863+     }
864+ 
865+     @Composable
866+     fun  ValidatingInputTextField (
867+         email :  String ,
868+         updateState :  (String ) ->  Unit ,
869+         validatorHasErrors :  Boolean 
870+     ) {
871+         val  viewModel =  EmailViewModel ()
872+         OutlinedTextField (
873+             modifier =  Modifier 
874+                 .fillMaxWidth()
875+                 .padding(10 .dp),
876+             value =  email,
877+             onValueChange =  updateState,
878+             label =  { Text (" Email"  ) },
879+             isError =  validatorHasErrors,
880+             supportingText =  {
881+                 if  (validatorHasErrors) {
882+                     Text (" Incorrect email format."  )
883+                 }
884+             }
885+         )
886+ 
887+ 
888+         ValidatingInputTextField (
889+             email =  viewModel.email,
890+             updateState =  { input ->  viewModel.updateEmail(input) },
891+             validatorHasErrors =  viewModel.emailHasErrors
892+         )
893+     }
894+ }
895+ //  [END android_compose_text_auto_format_phone_number_validatetext]
0 commit comments