简体   繁体   中英

How to clean data from web forms in asp.net mvc?

I have an asp.net mvc app and users are cutting and pasting data into textarea fields. We are getting some characters in there that my IBM U2 database does not like. How can I strip all of these out of the textarea before sending them to my database.

The problems yesterday were with the en dash and em dash. Today I'm having a problem with another character which I believe is a hex 1F.

Here is the code I added, but I think I need something to account for all bad characters.

Problem = result["notes"].Replace("\u2013", "-").Replace("\u2014", "-");

Any suggestions.

Well, the replace calls will probably be fine for a small number of characters, but if the input text is large (or the number of chars to strip out is large) then you are going to have an issue.

My suggestion would be to generate a RegEx instance which matches all the bad characters, and then call the Replace method on it, passing the string with the bad characters, as well as the replacement character for the bad characters (in this case, "-").

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