In any type of application user input validation is most
important if it fails your all over application is fails. So Web Developer has
to firstly think on the validation for their application.
Not only client side validation important, but also server
side validation is more important for highly secure data.
When you are developing application in MVC you can add
validation when assigning value to properties in model. In MVC data annotation is
useful for validating fields by using specifying attributes on the each field.
Below are some data annotation attributes:
Required
This attribute is used for mandatory fields.
public class Employee
{
[Required]
public string
FirstName
{
get;
set;
}
[Required]
public string LastName
{
get;
set;
}
}
When you decorate field
as “Required” it means it is mandatory. You can also specify error message for
the validation. When Validation fails this message is display to the user.
public class Employee
{
[Required(ErrorMessage="Firstname
is mandatory")]
public string
FirstName
{
get;
set;
}
}
If you not provide any
message it display message as “Firstname is required”.
StringLength:
If you have one textbox
and you want to add validation on the user input length you can use this
attribute like below:
public class Employee
{
[Required(ErrorMessage="Firstname
is mandatory")]
[StringLength(50,ErrorMessage="Firstname length cannot greater than 50
characters")]
public string
FirstName
{
get;
set;
}
}
In the above code “FirstName”
only accept for 50 characters else it gives error message specified in the attribute.
RegularExpression:
You can add validation
by using regular expression. If there is condition that firstname only accept
characters, in this case you can use this attribute like below:
public class Employee
{
[RegularExpression(@"^[a-zA-Z]+$",ErrorMessage="Only characters allowed in firstname")]
public string
FirstName
{
get;
set;
}
}
Range:
This attribute specifies
minimum and maximum length for the numerical fields. Let’s take example of age,
in age field we are not allow to enter age more than 100 and not below 0 see
below code, how to add attribute for this condition:
Public class Employee
{
[Required(ErrorMessage="Firstname
is mandatory")]
[RegularExpression(@"^[a-zA-Z]+$",ErrorMessage="Only characters allowed in firstname")]
public string
FirstName
{
get;
set;
}
[Required]
[Range(1,100,ErrorMessage="Age
must be between 1 to 100")]
public int Age
{
get;
set;
}
}
Compare:
This attribute are used for matching two values in the same model
by passing name of field with we want to compare. In many cases we compare
password with confirm password for this purpose we can use this attribute.
public class ChangePasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current
password")]
public string
OldPassword { get; set;
}
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.",
MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New
password")]
public string
NewPassword { get; set;
}
[DataType(DataType.Password)]
[Display(Name = "Confirm
new password")]
[Compare("NewPassword",
ErrorMessage = "The new password and
confirmation password do not match.")]
public string
ConfirmPassword { get; set;
}
}
See the above code where we added Compare attribute above
the ConfirmPassword field. In the first parameter we have mention the name of
the field with whom we want to compare this field that is “NewPassword”.
Below are some another Data Annotation attributes we can specify:
1. DataType
:
This attribute are decorate to field for specifying which
type of data it will accept if we are assigning to the property. Simply we can
set the datatype of the property. For the Password field we can set datatype as
a password.
Below DataType are supported in this attribute
1.
Currency
2.
Date
3.
DateTime
4.
Duration
5.
EmailAddress
6.
Html
7.
ImageURI
8.
PhoneNumber
9.
Password
10.
MultilineText
11.
Text
12.
URl
2. Display:
This Field used for display text for the textbox. Means
which type of text you have to enter in the textbox or any other control.
When you create the form using this model it shows the text
as label for the textbox.
public class Employee
{
[Display("First
Name")]
public string
FirstName
{
get;
set;
}
[Required]
[Display("Age")]
[Range(1,100,ErrorMessage="Age
must be between 1 to 100")]
public int Age
{
get;
set;
}
}
See below link for related
articles
1.
Getting Started with MVC
2. ViewBag, ViewData and TempData
3.
Folder Structure In MVC
4.
MVC Architecture Theory
5.
Data Annotations and Validations in MVC
6.
MVC Architecture: Traditional Routes
7.
MVC Architecture: Attribute based Routing
8.
Bundling and Minification in MVC
9.
AutoComplete in MVC
http://dotnet-root.blogspot.in/2016/10/autocomplete-using-aspnet-mvc-and-jquery.html
10.
Adding Controller, Model and Views
0 Comments