简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM