Richard Ev
Richard Ev

Reputation: 54137

Verifying that an uploaded file contains only plain text

I have an ASP.NET MVC application that allows the user to upload a file that should only contain plain text.

I am looking for a simple approach to validate that the file does indeed contain only text.

For my purposes I am happy to define text as any of the characters that I can see printed on my GB QWERTY keyboard.

Business rules mean that my uploaded file won't contain any accented characters, so it doesn't matter if the code accepts or rejects these.

Approaches so far that have not worked:

I'd rather avoid using a lengthy Regex pattern to get this to work.

Upvotes: 4

Views: 467

Answers (1)

Jeff Sternal
Jeff Sternal

Reputation: 48673

It sounds like you want ASCII characters 32-126 plus a few odds and ends like 9 (horizontal tab), carriage return & linefeed, etc..

I'd rather avoid using a lengthy Regex pattern to get this to work.

As long as that doesn't mean 'no regular expressions at all', you can use the accepted answer from this stack overflow question (I've added the horizontal tab character to the original):

^([^\x09\x0d\x0a\x20-\x7e\t]*)$

Upvotes: 2

Related Questions