python validate string allowed only spaces, alphabet and numbers

Using pattern matching, this is not hard to achieve.

To check that each character matches your requirements

Regular expression


With spaces checking:

"^[a-zA-Z0-9_ ]*$"

Code example:

if allow_special_char == False:
import re
if not re.match("^[a-zA-Z0-9_ ]*$", gname):
print "Invalid"

Regular expression refer from


By Keenlio, September 29, 2015

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

five + 4 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>