Skip to content

Instantly share code, notes, and snippets.

@OhhhThatVarun
Created July 13, 2021 09:23
Show Gist options
  • Save OhhhThatVarun/2b727d67977eb301b79de29a4f76eee6 to your computer and use it in GitHub Desktop.
Save OhhhThatVarun/2b727d67977eb301b79de29a4f76eee6 to your computer and use it in GitHub Desktop.
Demonstrating a traditional way to validate user input.
fun validateUserInput(): Boolean {
val emailPattern = Pattern.compile("my-perfect-regex")
val passwordPattern = Pattern.compile("my-perfect-regex")
if(email == null || email.length > 30 || !emailPattern.matcher(email).matches()) {
// Handle Email is invalid
} else if (password == null || password.length > 30 || !passwordPattern.matcher(password).matches()) {
// Handle Password is invalid
} else {
return true
}
return false
}
OR
fun validateUserInput(): Boolean {
val emailPattern = Pattern.compile("my-perfect-regex")
val passwordPattern = Pattern.compile("my-perfect-regex")
if(email == null) {
// Handle Email is empty
} else if(email.length > 30) {
// Handle Email length too long
} else if(!emailPattern.matcher(email).matches()) {
// Handle Email is invalid
} else if (password == null) {
// Handle Password is empty
} else if(!passwordPattern.matcher(password).matches()) {
// Handle Password is invalid
} else if (password.length > 30) {
// Handle Password length too long
} else {
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment