4

I'm trying to handle error cases in my android app while signing up users. I want to make sure the email address provided is valid and by valid I mean the correct format : "something@something.com".

I have searched on google and stackoverflow, but couldn't find an exact answer in Kotlin.

liverwort
  • 47
  • 1
  • 3
  • If Kotlin supports regular expressions you can google for that, there is plenty of info available online on how to use a regular expression to validate an email address – Joakim Danielson May 20 '19 at 05:11
  • https://gist.github.com/ironic-name/f8e8479c76e80d470cacd91001e7b45b I am not sure with the kotlin but normally we use regex to validate it . – Sachin Shetti May 20 '19 at 05:11
  • 1
    please check https://stackoverflow.com/a/7882950/5909412 – P.Juni May 20 '19 at 05:25

1 Answers1

23

Based on this answer in java (How should I validate an e-mail address?), you can use extension function to check the validity in kotlin.

fun String.isEmailValid(): Boolean {
    return !TextUtils.isEmpty(this) && android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()
}

and then call

val str = ""
str.isEmailValid()