Home > Mobile >  password must contain numbers
password must contain numbers

Time:11-09

So I'm building registration page type of thing in android studio kotlin and in it password length has to be over 8 symbols and it must contain at least one number otherwise it should not let me press register button

I managed to make it so that jts necessary for password lenght to be over 8 symbols but I cant seem to add the requirement of it containing number. I have tried to use contain(Int) but it gives me an error. I'd greatly appreciate the help. Thanks in advance

CodePudding user response:

In my opinion, it is easy to do this in Kotlin. Only we use contains("[0-9]".toRegex()), if there is no number in the password, it returns false and the condition is not fulfilled

Example

   if (password.contains("[0-9]".toRegex())) {
       //Write code here
      }

CodePudding user response:

Here's the kotlin.text package with all the standard library stuff for working with text: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/

There's nothing like contains(Int) in there, because it would be impossible to have a function where you pass any type and it somehow works out if the text "represents" an instance of that type somehow, right? Like how would contains(Bitmap) or contains(Dog) work?

So generally for this kind of thing, you have specialised functions that check for a specific type - no passing a type in as a parameter. For text, they're often called parse*, like parseInt. In Kotlin, the standard functions start with to* - in this case, toInt and toIntOrNull. They attempt to parse the string as an integer, and return that Int - if it can't be parsed as one, toInt throws an exception, toIntOrNull returns null instead. (There are other convert-to-number functions too, like toFloat)

Now you have a way to check if something is a number or not - call toIntOrNull() on it, and see if you get null (not a number). And you want to know if any of the characters in your string is a number, right? So you can do this:

val hasDigit = password.any { it.toIntOrNull() != null }

And if that's true for any of the characters, the result will be true. You could make it an extension function if you like:

val String.hasDigit get() = any { it.toIntOrNull() != null }

"password1".hasDigit
>> true

That's the general way of doing things. In the case of digits specifically, there's a nice isDigit() function on Chars you can use instead:

password.any { it.isDigit() }
// or if you like
password.any(Char::isDigit)

and of course you can use other standard functions like count if you want a minimum number of digits, etc.

Another thing you can do is create a lookup, and check if characters are in that:

// you can use a string
val allowed = "abcdefghijklmnopqrstuvwxyz!?*"
// or a list - lots of ways to build it!
val allowed = ('a'..'z')   listOf('!', '?')   '*'

// then check
password.all { it in allowed }

or Xayal's regex example - and then you can use any, all, none etc to lay down your rules. Once you're familiar with the standard library, you can combine all these functions to make more specific stuff - that's kinda the way it's designed

  • Related