What is String Length

In my last article I explained the Required, Range, Compare and Regular Expression validation. In this article I am going to explain the String Length validation.

When we need to specify the length of the string entered in the textbox then we can specify the StringLength Attribute.

Syntax of StringLength

	[StringLength(4,ErrorMessage="Name must not be more than 4 char")]
      public string stu_name { get; set; }


This 4 represent that we can enter maximum 4 characters.

Now execute this code and you will get following output:-

		string Length validation

		Figure 1

We can also specify the minimum and maximum length for the string in the following manner:-

		[StringLength(10, MinimumLength=4, ErrorMessage = "Name must be between 4 and 10 char")]
      public string stu_name { get; set; }


Now the minimum length of char is 4 and maximum length is 10

		string Length validation in MVC

		Figure 1

As you can see that now it’s taking char between 4 and 10.

Email Address

For any query you can send mail at info@techaltum.com