tags:

views:

28

answers:

1

I need to remove special characters from the posted data. It may be possible by using Regular Expressions or may be other. How to strip the special characters. Plz help

+2  A: 

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.

sebpiq
But my textbox is not in a form. And also I am not validating the form. Also I want only to remove the "(quotation) symbols.
Rajasekar
Can you be a little bit more precise ? Do you "simply" need to remove quotation marks ? Or quotation marks around a text ? Can you give more details ?!
sebpiq