Created
September 16, 2020 17:17
-
-
Save mksantoki/5ce449c9295706439a96f0597ac0b893 to your computer and use it in GitHub Desktop.
Kotlin validation extensions to validate email,phone number and many others
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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