Rajasekar
Rajasekar

Reputation: 18948

How can I remove special characters in posted data

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?

Upvotes: 1

Views: 726

Answers (1)

sebpiq
sebpiq

Reputation: 7802

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.

Upvotes: 3

Related Questions