Datatype and Compare Attribute

Compare Attribute in ASP.NET MVC

In this article, I am going to discuss DataType and Compare Attribute with some real time examples in ASP.NET MVC application.

Please read the below articles before proceeding to this article as I am going to use the same example that we started in our previous articles.

Data Annotations in ASP.NET MVC

Required and StringLength Attribute

Regular Expression Attribute

Range Attribute in ASP.NET MVC

Custom Validation Attribute in ASP.NET MVC

DataType Attribute:

DataType attributes enable 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 Email address, URL or a password. There are various data types that include Currency, Date, Time, Password and MultilineText etc.

Let’s see some of the examples of using 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 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:

Compare attribute is used to compare 2 properties of a model has 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 type 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, we will discuss the validation message and validation summary in ASP.NET MVC.

SUMMARY

In this article, I try to explain the Datatype and Compare Attribute in ASP.NET MVC application step by step with some real-time examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article. 

Leave a Reply

Your email address will not be published. Required fields are marked *