DataType and Compare Attributes in ASP.NET MVC

DataType and Compare Attributes in ASP.NET MVC Application

In this article, I am going to discuss DataType and Compare Attributes in ASP.NET MVC Application with Examples. Please read our previous article where we discussed how to create Custom Validation Attribute in ASP.NET MVC. At the end of this article, you will understand the need and use of Compare Attribute in the ASP.NET MVC Application.

DataType Attribute in ASP.NET MVC:

DataType Attribute in ASP.NET MVC Framework enables us to provide the runtime information about the specific purpose of the properties. For example, a property of type string can have various scenarios as it might hold an Email address, URL, or password. There are various data types that include Currency, Date, Time, Password and MultilineText, etc. Let’s see some of the examples of using the DataType attribute.

[DataType(DataType.PostalCode, ErrorMessage = "Please Enter a valid PIN/ZIP Code")]
public string PostalCode
{
    get;
    set;
}

[DataType(DataType.Url, ErrorMessage = "Please Enter a valid URL")]
public string URL
{
    get;
    set;
}

[DataType(DataType.Password)]
public string Password
{
    get;
    set;
}
[DataType(DataType.PhoneNumber, ErrorMessage = "Please Enter a valid Phone Number")]
public string Mobile
{
    get;
    set;
}
Password:

When we set the DataType as a password, we will see the password field in a non-readable format. Let’s add Password Attribute to Password and Confirm Password property of employee model as shown below

[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is Required")]
public string Password
{
    get;
    set;
}
[DataType(DataType.Password)]
[Required(ErrorMessage = "Confirm Password is Required")]
public string ConfirmPassword
{
    get;
    set;
}

When we run the application and enter the data into the password and confirm password textbox, it will be shown as below.

Compare Attribute in ASP.NET MVC

Compare Attribute in ASP.NET MVC Application:

Compare Attribute in ASP.NET MVC Framework is used to compare 2 properties of a model that have the same value. Comparing email addresses and passwords is the common use case of Compare attribute. Let’s understand using Compare attribute with an example. For example, to ensure that the user has typed the correct password we must use Password and ConfirmPassword of employee model as shown below.

[DataType(DataType.Password)]1
[Required(ErrorMessage = "Password is Required")]
public string Password
{
    get;
    set;
}
[DataType(DataType.Password)]
[Required(ErrorMessage = "Confirm Password is Required")]
[Compare("Password", ErrorMessage = "Password and Confirm Password do not match"]
public string ConfirmPassword
{
    get;
    set;
}

If both Password and Confirm Password are not the same, the user will get the model validation error, as shown below:

Compare Attribute in ASP.NET MVC

In the next article, I am going to discuss the Validation Message and Validation Summary in ASP.NET MVC Application. Here, In this article, I try to explain the Datatype and Compare Attributes in ASP.NET MVC application with examples. I hope this Datatype and Compare Attributes in the ASP.NET MVC article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this Datatype and Compare Attributes in the MVC article.