Check if TextfieldValue is accepting space as characters

218 Views Asked by At

I am using OutlineTextField and the value is taken from TextFieldValue. I have a save button on top of it which gets enabled if there is entered text and disabled vice versa. I have validated using below code enter image description here

val textFieldState = remember {
mutableStateOf(TextFieldValue(EMPTY))
}

onSaveClickEnabled = textFieldState.value.text.isNotEmpty(),

But the above code is not validating if space entered initially. It enables SAVE button even I enter space. I tried doing trim but no luck.

So I need to validate in a way that if space and character entered together then only it should enable save button otherwise if only space entered, it should keep the save as disabled.

2

There are 2 best solutions below

0
Ionut On

You can use the String class function isBlank()

This will return true if string is empty or if it only contains WhiteSpace characters.

So in your case

textFieldState.value.text.isBlank()
0
Megh Lath On

Try this:

var onSaveClickEnabled by remember {
    mutableStateOf(false)
}
val textFieldState = remember {
    mutableStateOf("")
}

onSaveClickEnabled = textFieldState.value.trim() != ""

.....

OutlinedTextField(
        value = textFieldState.value,
        onValueChange = {
            textFieldState.value = it
            onSaveClickEnabled = it.trim() != ""
        },
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 5.dp),
        .....
    )

enter image description here