Skip to content

Instantly share code, notes, and snippets.

@gsrathoreniks
Created December 6, 2020 12:33
Show Gist options
  • Save gsrathoreniks/d46f02be68c0e4869d12b98c9499ed3c to your computer and use it in GitHub Desktop.
Save gsrathoreniks/d46f02be68c0e4869d12b98c9499ed3c to your computer and use it in GitHub Desktop.
ValidationUtil.kt
package `in`.gsrathoreniks.validationdemo
import androidx.appcompat.widget.AppCompatEditText
import java.util.regex.Pattern
object ValidationUtil {
fun isValidUsername(view: AppCompatEditText, username: String?, regex: String = "^[a-zA-Z0-9._-]{3,20}$"): Boolean {
view.requestFocus()
when {
isNullOrEmpty(username) -> view.error = "Enter Username"
!Pattern.matches(regex, username) -> view.error = "Please enter a valid Username"
else -> return false
}
return true
}
fun isNullOrEmpty(input: String?): Boolean = input == null || input.isEmpty()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment