How can I remove special characters in posted data
I need to remove special characte开发者_JAVA技巧rs from the posted data. It may be possible by using Regular Expressions or may be other. How to strip the special characters?
You can use form validation for this http://docs.djangoproject.com/en/dev/ref/forms/validation/:
class MyForm(Form):
def clean_<fieldname>(self):
#your validation
And here is the method you can use to remove special characters :
import re
cleaned_field_value = re.sub(r'\W', '', raw_field_value)
However, this will not remove underscores if you need to remove them, use the regular exp :
r'\W|_'
instead.
EDIT :
If it is just a textbox, so forget the form validation method... But I guess the sub
method is still valid.
精彩评论