StringLength Data Annotation attribute in MVC 3 Razor
- The StringLength attribute specifies the maximum length of string that is acceptable.
- Validation message is thrown for the string length greater than specified using this property.
- The StringLength attribute ensures the string value provided by the customer will fit in the database.
Example 1 :
In the above screenshot we have applied StringLength attribute to the FirstName property. When user will enter FirstName greater than 10 characters, validation message will be thrown.
UI :
When the form is posted with FirstName length greater than 10, validation message is shown. The validation message shown is the default message.
Example 2 :
In the above example, along with specifying the string length we have also provided the validation message to show on UI.
Example 3 :
In the above example, we are picking the error message from resource file. The preceding code expects to have a resource file with name Multi.resx with key FirstName_Length.
UI :
Example 4 :
The MinimumLength is an optional, named parameter you can use to specify the minimum length for a string. The above code requires the FirstName property to contain a string with five or more characters and less than 10 characters for validation to pass.
UI :
0 comments:
Post a Comment