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

"^[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

By Keenlio, September 29, 2015

What do you think?

Leave a Reply

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


+ 6 = seven

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>