The StringLength
attribute in C# is used to specify the maximum and minimum length of a string property in a class. The ErrorMessage
property of the StringLength
attribute is used to specify a custom error message that is displayed when the length of the string property violates the specified range.
The ErrorMessage
property can include placeholders for the following parameters:
{0}
: The name of the validated property.{1}
: The minimum length allowed for the property.{2}
: The maximum length allowed for the property.Here's an example of how to use the StringLength
attribute with a custom error message:
public class Person { [StringLength(50, MinimumLength = 2, ErrorMessage = "{0} must be between {1} and {2} characters.")] public string Name { get; set; } }
In this example, the Name
property of the Person
class is annotated with the StringLength
attribute, which specifies that the maximum length of the property is 50 characters and the minimum length is 2 characters. The ErrorMessage
property of the attribute includes placeholders for the name of the property, the minimum length, and the maximum length.
If the Name
property is too short or too long, the custom error message is displayed with the actual values of the parameters substituted for the placeholders. For example, if the Name
property is set to "A"
, the error message would be "Name must be between 2 and 50 characters."
.
Understanding ErrorMessage parameter in StringLength Attribute in C#
[StringLength(50, ErrorMessage = "The field must be a maximum of 50 characters.")] public string MyProperty { get; set; }
Stringlength attribute errormessage parameter example
[StringLength(10, ErrorMessage = "The field must be a maximum of 10 characters.")] public string MyProperty { get; set; }
C# StringLength attribute ErrorMessage usage
[StringLength(20, ErrorMessage = "The field must be a maximum of 20 characters.")] public string MyProperty { get; set; }
How to set custom error message with StringLength attribute in C#
[StringLength(15, ErrorMessage = "Custom error message for length validation.")] public string MyProperty { get; set; }
C# StringLength attribute ErrorMessage parameter syntax
[StringLength(25, ErrorMessage = "Custom error message.")] public string MyProperty { get; set; }
Custom validation message with StringLength attribute in C#
[StringLength(30, ErrorMessage = "Custom error message for string length validation.")] public string MyProperty { get; set; }
StringLength attribute in C# with custom error message
[StringLength(40, ErrorMessage = "Custom error message for string length.")] public string MyProperty { get; set; }
Setting validation error message with StringLength attribute in C#
[StringLength(35, ErrorMessage = "Custom error message for validation.")] public string MyProperty { get; set; }
C# StringLength attribute ErrorMessage parameter best practices
[StringLength(45, ErrorMessage = "Best practice error message.")] public string MyProperty { get; set; }
String length validation error handling with StringLength attribute in C#
[StringLength(55, ErrorMessage = "Error: Invalid string length.")] public string MyProperty { get; set; }
oracle12c c# directive amazon-sagemaker virtualenv index-error angular-router-guards slave kotlin-interop delivery-pipeline