To pre-validate, you can keep it simple like this:
fun main() { val emails = listOf("john@example.com", "John Doe") emails.forEach { println("input=$it isEmail=${it.isEmail()}")} // input=john@example.com isEmail=true // input=John Doe isEmail=false val phones = listOf(" 5555555555 ", "+1 (555) 555-5555", "555 - 555 - 5555", "invalid") phones.forEach { println(it.normalizePhoneNumber()) } // 5555555555 // 15555555555 // 5555555555 // null } fun String.isEmail(): Boolean = length > 6 && contains("@") fun String.normalizePhoneNumber(): String? = this.filter { it.isDigit() }.takeIf { it.length > 6 }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
To pre-validate, you can keep it simple like this: