Created
July 13, 2021 09:23
-
-
Save OhhhThatVarun/2b727d67977eb301b79de29a4f76eee6 to your computer and use it in GitHub Desktop.
Demonstrating a traditional way to validate user input.
This file contains hidden or 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
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