Skip to content

Instantly share code, notes, and snippets.

@mksantoki
Created September 16, 2020 17:17
Show Gist options
  • Save mksantoki/5ce449c9295706439a96f0597ac0b893 to your computer and use it in GitHub Desktop.
Save mksantoki/5ce449c9295706439a96f0597ac0b893 to your computer and use it in GitHub Desktop.
Kotlin validation extensions to validate email,phone number and many others
import android.text.TextUtils
import android.widget.EditText
import androidx.appcompat.widget.AppCompatEditText
fun String.isEmpty(): Boolean {
return (TextUtils.isEmpty(this)
|| this.equals("", ignoreCase = true)
|| this.equals("{}", ignoreCase = true)
|| this.equals("null", ignoreCase = true)
|| this.equals("undefined", ignoreCase = true))
}
fun AppCompatEditText.isValidEmail(): Boolean {
val emailPattern = Regex("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")
return !this.text.toString().isEmpty() && this.text.toString().matches(emailPattern)
}
fun EditText.isValidEmail(): Boolean {
val emailPattern = Regex("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")
return !this.text.toString().isEmpty() && this.text.toString().matches(emailPattern)
}
fun String.isValidEmail(): Boolean {
val emailPattern = Regex("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")
return !this.isEmpty() && this.matches(emailPattern)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment