package com.fsck.k9

import android.text.util.Rfc822Tokenizer
import android.widget.AutoCompleteTextView.Validator

import java.util.regex.Pattern

class EmailAddressValidator : Validator {

    override fun fixText(invalidText: CharSequence): CharSequence = ""

    override fun isValid(text: CharSequence): Boolean = Rfc822Tokenizer.tokenize(text).isNotEmpty()

    fun isValidAddressOnly(text: CharSequence): Boolean = EMAIL_ADDRESS_PATTERN.matcher(text).matches()

    companion object {
        private val EMAIL_ADDRESS_PATTERN = Pattern.compile(
                "[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}" +
                        ")+"
        )
    }
}

package com.fsck.k9

import android.text.util.Rfc822Tokenizer
import android.widget.AutoCompleteTextView.Validator

import java.util.regex.Pattern

class EmailAddressValidator : Validator {

    override fun fixText(invalidText: CharSequence): CharSequence = ""

    override fun isValid(text: CharSequence): Boolean = Rfc822Tokenizer.tokenize(text).isNotEmpty()

    fun isValidAddressOnly(text: CharSequence): Boolean = EMAIL_ADDRESS_PATTERN.matcher(text).matches()

    companion object {

        //https://www.rfc-editor.org/rfc/rfc2396.txt (3.2.2)

        private const val ALPHA = "[a-zA-Z]"
        private const val ALPHANUM = "[a-zA-Z0-9]"
        private const val TOP_LABEL = "(($ALPHA($ALPHANUM|\\-|_)*$ALPHANUM)|$ALPHA)"
        private const val DOMAIN_LABEL = "(($ALPHANUM($ALPHANUM|\\-|_)*$ALPHANUM)|$ALPHANUM)"
        private const val HOST_NAME = "((($DOMAIN_LABEL\\.)+$TOP_LABEL)|$DOMAIN_LABEL)"

        private val EMAIL_ADDRESS_PATTERN = Pattern.compile(
                "[a-zA-Z0-9\\+\\.\\_\\%\\-]{1,256}" +
                        "\\@$HOST_NAME"
        )
    }
}

Validate hostname part of email address according to rfc2396
Validate hostname part of email address according to rfc2396

cketti/k-9,cketti/k-9,cketti/k-9,k9mail/k-9,k9mail/k-9,k9mail/k-9,cketti/k-9