Search code examples
kotlinkotlin-multiplatformkotlin-multiplatform-mobile

How should I validate an email address in Kotlin multiplatform mobile(KMM)?


How should we validate Kotlin multiplatform mobile(KMM)? I found this question but it used java.util.regex.Pattern and it is not useful for KMM:

How should I validate an e-mail address?


Solution

  • You can use the Kotlin Regex class to validate an email address

     /**
     * Email address pattern, same as [android.util.Patterns.EMAIL_ADDRESS]
     */
    private val emailAddressRegex = Regex(
        "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                "\\@" +
                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                "(" +
                "\\." +
                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                ")+"
    )
    
    
    if (email.matches(emailAddressRegex)){
       //Valid email address
    }