Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string is a valid email in android? [duplicate]

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 : "[email protected]".

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

like image 343
liverwort Avatar asked Dec 02 '22 09:12

liverwort


1 Answers

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()
like image 114
Romain Goutte-Fangeas Avatar answered Dec 28 '22 06:12

Romain Goutte-Fangeas