python validate string allowed only spaces, alphabet and numbers

September 29, 2015

Using pattern matching, this is not hard to achieve. To check that each character matches your requirements Regular expression “^[a-zA-Z0-9_]*$” 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 http://stackoverflow.com/a/336220/1903116 reference: http://stackoverflow.com/questions/19970532/how-to-check-a-string-for-a-special-character