0

There is an outlinedtextfield where the user enters his/her name and surname, and this outlinetextfield enters the user name and surname, but if the user's name and surname are less than 3 characters, I want to make the border of the outlinetextfield red, but I couldn't figure out how to do this control because space intervened.

for example :

a(space)b wrong
jac(space)jac correct
tony(space)stark correct

this is my example code:

OutlinedTextField(
                    value = state.name,
                    onValueChange = { onChangeName(it) },
                    modifier = Modifier
                        .fillMaxWidth(),
                    shape = RoundedCornerShape(25.dp),
                    label = {
                        Text(
                            text = "name and lastname",
                            fontSize = 14.sp,
                            color = Color.White
                        )
                    },
                    colors = TextFieldDefaults.outlinedTextFieldColors(
                        focusedBorderColor = if (state.name.length < 7) Color.Red else DefaultDYTColor,
                        unfocusedBorderColor = Color.Transparent
                    )
                ) 

When I do it this way, it accepts it as correct even if I type 7 characters without spaces, but it should not be like this.

for example:

tonystark 

According to the code I wrote, this is correct because it is greater than 7 characters

How to achieve this issue ? Do you have any suggestion or solve ?

NewPartizal
  • 604
  • 4
  • 18
  • It is not clear what you want to achieve. Do you want to split the text and each word should have at least 3 characters? – Gabriele Mariotti Feb 08 '23 at 11:00
  • for example user enter name and last name in textfield. user's name and last name should have at least 3 characters. – NewPartizal Feb 08 '23 at 11:20
  • Check the Jan Bína's answer. Note that someone can have 3 names, someone can have a surname with 2 characters.... – Gabriele Mariotti Feb 08 '23 at 11:32
  • yeah thats sound good I didnt think about it. I am enlightened now. I guess Jan Bína's answer is correct but besides Jan Bína's answer how can I make a logic what you said like in this case – NewPartizal Feb 08 '23 at 11:46

1 Answers1

0

This is just an algorithmic problem, you can solve it like this:

fun isValidName(name: String): Boolean {
    // split name on each space and filter out those that are blank (consecutive spaces)
    val splits = name.split(" ").filter { it.isNotBlank() }
    // we need at least 2 strings (name and surname)
    // you can also use != 2 if you want exactly 2
    if (splits.size < 2) return false
    // if any name is less than 3 chars, return false
    for (split in splits) {
        if (split.trim().length < 3) {
            return false
        }
    }
    // now we have at least 2 names and all of them have 3 or more chars
    return true
}
Jan Bína
  • 3,447
  • 14
  • 16
  • You could use the `any` method and a shorthand method to have a cleaner code : `fun isValidName(name: String): Boolean = name .split(" ") .filter { it.isNotBlank() } .any { it.trim().length < 3 }` – Jolan DAUMAS Feb 08 '23 at 10:37
  • yeah sure, but I wanted more "basic" solution, since OP is clearly beginner – Jan Bína Feb 08 '23 at 10:42