VillaRava
VillaRava

Reputation: 167

Check if string has at least one alphanumeric character

I'm an absolute beginner trying to learn string validation. I have a variable about to store user input:

Text_input = raw_input('Type anything: ')

I want to check if Text_input contains at least one alphanumeric character. (If not, the program should print a message such as "Try again!" and ask the user to type again.) So, typing "A#" should pass but "#" should not. Any suggestions?

Upvotes: 3

Views: 5149

Answers (1)

VillaRava
VillaRava

Reputation: 167

This worked for me:

    Text_input = raw_input('Type anything: ')
    if any(char.isalpha() or char.isdigit() for char in Text_input):
        print "Input contains at least one alphanumeric character."
    else:
        print "Input must contain at least one alphanumeric character." 

Upvotes: 7

Related Questions