Home > Back-end >  How to decrease the lines of code for input validation?
How to decrease the lines of code for input validation?

Time:01-26

I am validating my editText for incorrect user input. The code is as follows:

if (text.isNotEmpty() && !Pattern.matches(
            "[a-zA-Z] ",
            text
        ) && !text.contains(",") && !Pattern.matches(
            "[А-яЁё][-А-яЁё] ", text
        ) && !text.contains("@") && !text.contains(" ")
    )

I would like to reduce the number of lines, is it possible to check for @ , character entry at once, without writing text.contains for each? I know I could have simply included the android:digits in XML, but user may copy paste the input,so decided to validate. Thank you

CodePudding user response:

is it possible to check for @ , character entry at once, without writing text.contains for each?

You can use the none function.

if(text.none { it in ",@ " } && otherConditions)
  •  Tags:  
  • Related