Created
December 6, 2020 12:33
-
-
Save gsrathoreniks/d46f02be68c0e4869d12b98c9499ed3c to your computer and use it in GitHub Desktop.
ValidationUtil.kt
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
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