Skip to content

Instantly share code, notes, and snippets.

@ajithvgiri
Created June 1, 2021 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajithvgiri/b0ad4d2683eed0816638f3ca4472e03e to your computer and use it in GitHub Desktop.
Save ajithvgiri/b0ad4d2683eed0816638f3ca4472e03e to your computer and use it in GitHub Desktop.
formvalidation example
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