Created
June 1, 2021 17:16
-
-
Save ajithvgiri/b0ad4d2683eed0816638f3ca4472e03e to your computer and use it in GitHub Desktop.
formvalidation example
This file contains 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
private var email = MutableLiveData<String>().apply { value = "" } | |
private var password = MutableLiveData<String>() | |
var emailErrorMsg = MutableLiveData<String>() | |
var passwordErrorMsg = MutableLiveData<String>() | |
var isEmailErrorMsgEnabled = MutableLiveData<Boolean>() | |
var isPasswordErrorMsgEnabled = MutableLiveData<Boolean>() | |
fun performValidation(){ | |
var emailError = "" | |
var passwordError = "" | |
when { | |
email.value.isNullOrEmpty() -> { | |
emailErrorMsg.value = "Field can't be empty" | |
} | |
EMAIL_ADDRESS.matcher(email.value).matches().not() -> { | |
emailErrorMsg.value ="Invalid Email Address" | |
} | |
} | |
when { | |
password.value.isNullOrEmpty() -> { | |
passwordErrorMsg.value = "Field can't be empty" | |
} | |
password.value!!.length in 6..20 -> { | |
passwordErrorMsg.value = "The password length should be more than 4 letters" | |
} | |
} | |
isEmailErrorMsgEnabled.value = emailError.isEmpty().not() | |
isPasswordErrorMsgEnabled.value = passwordError.isEmpty().not() | |
if (emailError.isEmpty() && passwordError.isEmpty()){ | |
// perform login action | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment