Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how validate allow characters & special character but not allow number with character or number in kotlin

fun main() {
val regex = Regex("^[a-zA-Z\\s!@#\$%^&*()-_=+\\\\|\\[{\\]};:'\",<.>/?]*\$")

val names = listOf("John Doe", ".Smith", "Alice.", "Doe#Smith", "Doe#Smith99", "65464546499", "MM66M")

for (name in names) {
    if (!name.matches(Regex(".*\\d.*")) || regex.matches(name)) {
        println("$name is valid")
    } else {
        println("$name is invalid")
    }
}

}

here we expect result
John Doe is valid
.Smith is valid
Alice. is valid
Doe#Smith is valid
Doe#Smith99 is invalid
65464546499 is invalid
MM66M is invalid

but result come all values valid . what problem here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You just need to use and instead of or in the if statement
I edited it for you

fun main() {
    val regex = Regex("^[a-zA-Z\\s!@#\$%^&*()-_=+\\\\|\\[{\\]};:'\",<.>/?]*\$")

    val names = listOf("John Doe", ".Smith", "Alice.", "Doe#Smith", "Doe#Smith99", "65464546499", "MM66M")

    for (name in names) {
        if (!name.matches(Regex(".*\\d.*")) && regex.matches(name)) {
            println("$name is valid")
        } else {
            println("$name is invalid")
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading