Ecnalyr
Ecnalyr

Reputation: 5802

I need to validate that a submitted MIME type does not have a newline character using regex

I have tried:

[RegularExpression(@"\n", ErrorMessage = "Error")] // Needs to not allow newline characters
public string ImageMimeType { get; set; }

but it always fails when I attempt to upload a legitimate .bmp file.

I am not sure how to format this line appropriately to prevent newline characters.

Also - are there any other considerations I should take when validating a MIME type?

Solved

[RegularExpression(@"[^\n]+", ErrorMessage = "Error")]

Upvotes: 0

Views: 123

Answers (1)

Pavan Manjunath
Pavan Manjunath

Reputation: 28575

You are just specifying \n as the regex. Your legitimate files will not be matching \n and hence they are treated as NO_MATCH. You want it the other way. ie the filename mustn't contain \n. Try

[^\n]+

This says, match a string of 1-any number of characters ( you can specify string range using {m,n} syntax if you wish to) not containing a newline character.

Upvotes: 2

Related Questions