Search code examples
c#stringvalidationattribute

ValidationAttribute.FormatErrorMessage throwing Input string was not in a correct format error


I have written a custom attribute. In the error message I have special characters. due to these special characters, ValidationAttribute.FormatErrorMessage(GetDisplayName()) throwing Input string was not in a correct format error.

My error message is @"Your characters should be in: ^_{}~".

How can I fix this issue?


Solution

  • The error is because of {}. It has special meaning in c#. So you have to use escape character for that. ex: (ErrorMessage = "Your characters should be in: !#$%&()*+,-./:;<=>?@[]^_{{|}}~"

    So for escaping {, you have to use double {.